KPIT Yet To Onboard Assignment - Week - 2
KPIT Yet To Onboard Assignment - Week - 2
1
Chapter-4: More Complex Decision Making
Q1.
#include <stdio.h>
int main() {
float a, b, c;
printf("Enter the length of the first side: ");
scanf("%f", &a);
printf("Enter the length of the second side: ");
scanf("%f", &b);
printf("Enter the length of the third side: ");
scanf("%f", &c);
if (a == b && b == c) {
printf("The triangle is an equilateral triangle.\n");
}
else if (a == b || b == c || a == c) {
if ((a * a + b * b == c * c) ||
(a * a + c * c == b * b) ||
(b * b + c * c == a * a)) {
printf("The triangle is an isosceles and right-angled
triangle.\n");
}
else {
printf("The triangle is an isosceles triangle.\n");
}
}
else if ((a * a + b * b == c * c) ||
(a * a + c * c == b * b) ||
(b * b + c * c == a * a)) {
return 0;
}
OUTPUT
Enter the length of the first side: 4
Enter the length of the second side: 6
Enter the length of the third side: 3
The triangle is a scalene triangle.
Enter the length of the first side: 5
Enter the length of the second side: 5
Enter the length of the third side: 5
The triangle is an equilateral triangle.
Q2
3
CODE
#include <stdio.h>
#include <cmath>
int main() {
int R, G, B;
float C, M, Y, K;
float White;
if (R == 0 && G == 0 && B == 0) {
C = 0;
M = 0;
Y = 0;
K = 1;
}
else {
printf("CMYK values:\n");
printf("Cyan (C) = %.2f\n", C);
printf("Magenta (M) = %.2f\n", M);
printf("Yellow (Y) = %.2f\n", Y);
printf("Black (K) = %.2f\n", K);
return 0;
}
OUTPUT
Enter the Red (R) value (0-255): 56
4
Enter the Green (G) value (0-255): 90
Enter the Blue (B) value (0-255): 76
CMYK values:
Cyan (C) = 0.38
Magenta (M) = 0.00
Yellow (Y) = 0.16
Black (K) = 0.65
Q3
5
CODE
#include <stdio.h>
int main() {
if (hardness > 50 && carbonContent < 0.7 && tensileStrength > 5600)
{
grade = 10;
}
else if (hardness > 50 && carbonContent < 0.7) {
grade = 9;
}
else if (carbonContent < 0.7 && tensileStrength > 5600) {
grade = 8;
}
else if (hardness > 50 && tensileStrength > 5600) {
grade = 7;
}
else if (hardness > 50 || carbonContent < 0.7 || tensileStrength >
5600) {
grade = 6;
}
else {
grade = 5;
}
return 0;
}
OUTPUT
Enter the hardness of the steel: 50
Enter the carbon content of the steel: 0.4
Enter the tensile strength of the steel: 6000
6
The grade of the steel is: 8
Q4
CODE
#include <stdio.h>
int main() {
return 0;
}
OUTPUT
Enter weight (in kg): 60.20
Enter height (in meters): 1.78
Your BMI is: 19.00
BMI Category: Ideal
8
Chapter-5: Loop Control Instruction
Q1.
CODE
#include <stdio.h>
int main() {
int ascii_value = 0;
printf("ASCII Value\tCharacter\n");
printf("--------------------------\n");
return 0;
}
OUTPUT
ASCII Value Character
--------------------------
0
1 ☺
2 ☻
3 ♥
4 ♦
5 ♣
6 ♠
7
8
9
9
10
11
12
13
14
15
16 ►
17 ◄
18 ↕
19 ‼
20 ¶
21 §
22 ▬
23 ↨
24 ↑
25 ↓
26 →
27
8 ∟
29 ↔
30 ▲
31 ▼
32
33 !
34 "
35 #
36 $
10
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
58 :
59 ;
60 <
61 =
62 >
63 ?
64 @
65 A
66 B
67 C
68 D
11
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
91 [
92 \
93 ]
94 ^
95 _
96 `
97 a
98 b
99 c
100 d
12
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z
123 {
124 |
125 }
126 ~
127
128 Ç
129 ü
130 é
131 â
132 ä
13
133 à
134 å
135 ç
136 ê
137 ë
138 è
139 ï
140 î
141 ì
142 Ä
143 Å
144 É
145 æ
146 Æ
147 ô
148 ö
149 ò
150 û
151 ù
152 ÿ
153 Ö
154 Ü
155 ¢
156 £
157 ¥
158 ₧
159 ƒ
160 á
161 í
162 ó
163 ú
164 ñ
14
165 Ñ
166 ª
167 º
168 ¿
169 ⌐
170 ¬
171 ½
172 ¼
173 ¡
174 «
175 »
176 ░
177 ▒
178 ▓
179 │
180 ┤
181 ╡
182 ╢
183 ╖
184 ╕
185 ╣
186 ║
187 ╗
188 ╝
189 ╜
190 ╛
191 ┐
192 └
193 ┴
194 ┬
195 ├
196 ─
15
197 ┼
198 ╞
199 ╟
200 ╚
201 ╔
202 ╩
203 ╦
204 ╠
205 ═
206 ╬
207 ╧
208 ╨
209 ╤
210 ╥
211 ╙
212 ╘
213 ╒
214 ╓
215 ╫
216 ╪
217 ┘
218 ┌
219 █
220 ▄
221 ▌
222 ▐
223 ▀
224 α
225 ß
226 Γ
227 π
228 Σ
16
229 σ
230 µ
231 τ
232 Φ
233 Θ
234 Ω
235 δ
236 ∞
237 φ
238 ε
239 ∩
240 ≡
241 ±
242 ≥
243 ≤
244 ⌠
245 ⌡
246 ÷
247 ≈
248 °
249 ∙
250 ·
251 √
252 ⁿ
253 ²
254 ■
255
Q2
17
CODE
#include <stdio.h>
int main() {
int i, copy, remainder, sum;
while (copy != 0) {
remainder = copy % 10;
sum += remainder * remainder * remainder;
copy /= 10;
}
if (sum == i) {
printf("%d\n", i);
}
}
return 0;
}
OUTPUT
Armstrong numbers between 1 and 500 are:
1
153
370
371
407
18
Q3
CODE
#include<stdio.h>
#include<conio.h>
int main()
{
int match_sticks = 21, user_choice, computer_choice;
while(match_sticks>=1)
{
printf("Total Match Sticks: %d\n", match_sticks);
printf("Pick up the match sticks between (1 to 4): ");
scanf("%d", &user_choice);
if(user_choice>4)
{
printf("Invalid Entry");
break;
}
computer_choice = 5 - user_choice;
OUTPUT
Total Match Sticks: 21
Pick up the match sticks between (1 to 4): 3
Computer picks up the 2 match sticks.
Total Match Sticks: 16
Pick up the match sticks between (1 to 4): 2
Computer picks up the 3 match sticks.
Total Match Sticks: 11
Pick up the match sticks between (1 to 4): 4
Computer picks up the 1 match sticks.
Total Match Sticks: 6
Pick up the match sticks between (1 to 4): 4
Computer picks up the 1 match sticks.
Computer Wins.
Q4
CODE
#include <stdio.h>
int main() {
int number, positiveCount = 0, negativeCount = 0, zeroCount = 0;
char choice;
do {
if (number > 0) {
positiveCount++;
} else if (number < 0) {
20
negativeCount++;
} else {
zeroCount++;
}
return 0;
}
OUTPUT
Enter a number: 324
Do you want to enter another number? (y/n): y
Enter a number: 455
Do you want to enter another number? (y/n): y
Enter a number: -47
Do you want to enter another number? (y/n): y
Enter a number: 0
Do you want to enter another number? (y/n): y
Enter a number: 0
Do you want to enter another number? (y/n): n
Q5
21
CODE
#include <stdio.h>
int main() {
int decimal, quotient, octal, i, j;
quotient = decimal;
octal = 0;
i = 1;
while (quotient != 0) {
octal += (quotient % 8) * i;
i *= 10;
quotient /= 8;
}
return 0;
}
OUTPUT
Enter an integer: 4567
Octal equivalent: 10727
Q6
CODE
#include <stdio.h>
int main() {
int n, i;
22
int smallest, largest, num;
scanf("%d", &num);
smallest = largest = num;
return 0;
}
OUTPUT
Enter the number of elements: 5
Enter 5 numbers:
342
675
45435
6756
3
Range of the numbers: 45432
23
Chapter-6: More Complex Repetitions
Q1.
CODE
#include <stdio.h>
int main() {
int number, i;
return 0;
}
24
OUTPUT
Enter the number : 5
5*1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Q2
CODE
#include <stdio.h>
int main() {
double y, x, i;
printf("y\t\tx\t\ti\n");
printf("-----------------------------\n");
return 0;
}
OUTPUT
y x i
-----------------------------
1.0 5.5 5.75
1.0 6.0 6.00
1.0 6.5 6.25
1.0 7.0 6.50
1.0 7.5 6.75
1.0 8.0 7.00
1.0 8.5 7.25
1.0 9.0 7.50
1.0 9.5 7.75
1.0 10.0 8.00
1.0 10.5 8.25
1.0 11.0 8.50
1.0 11.5 8.75
1.0 12.0 9.00
1.0 12.5 9.25
2.0 5.5 6.75
2.0 6.0 7.00
2.0 6.5 7.25
2.0 7.0 7.50
2.0 7.5 7.75
2.0 8.0 8.00
2.0 8.5 8.25
2.0 9.0 8.50
2.0 9.5 8.75
26
2.0 10.0 9.00
2.0 10.5 9.25
2.0 11.0 9.50
2.0 11.5 9.75
2.0 12.0 10.00
2.0 12.5 10.25
3.0 5.5 7.75
3.0 6.0 8.00
3.0 6.5 8.25
3.0 7.0 8.50
3.0 7.5 8.75
3.0 8.0 9.00
3.0 8.5 9.25
3.0 9.0 9.50
3.0 9.5 9.75
3.0 10.0 10.00
3.0 10.5 10.25
3.0 11.0 10.50
3.0 11.5 10.75
3.0 12.0 11.00
3.0 12.5 11.25
4.0 5.5 8.75
4.0 6.0 9.00
4.0 6.5 9.25
4.0 7.0 9.50
4.0 7.5 9.75
4.0 8.0 10.00
4.0 8.5 10.25
4.0 9.0 10.50
4.0 9.5 10.75
4.0 10.0 11.00
4.0 10.5 11.25
27
4.0 11.0 11.50
4.0 11.5 11.75
4.0 12.0 12.00
4.0 12.5 12.25
5.0 5.5 9.75
5.0 6.0 10.00
5.0 6.5 10.25
5.0 7.0 10.50
5.0 7.5 10.75
5.0 8.0 11.00
5.0 8.5 11.25
5.0 9.0 11.50
5.0 9.5 11.75
5.0 10.0 12.00
5.0 10.5 12.25
5.0 11.0 12.50
5.0 11.5 12.75
5.0 12.0 13.00
5.0 12.5 13.25
6.0 5.5 10.75
6.0 6.0 11.00
6.0 6.5 11.25
6.0 7.0 11.50
6.0 7.5 11.75
6.0 8.0 12.00
6.0 8.5 12.25
6.0 9.0 12.50
6.0 9.5 12.75
6.0 10.0 13.00
6.0 10.5 13.25
6.0 11.0 13.50
6.0 11.5 13.75
28
6.0 12.0 14.00
6.0 12.5 14.25
Q3
CODE
#include <stdio.h>
#include <math.h>
int main() {
double p, r, a;
int n, q, i;
return 0;
}
OUTPUT
Enter principal (p), rate of interest (r), number of years (n), and number of times interest is
compounded per year (q) for set 1: 658
10
2
29
2
The amount for set 1 is: 799.80
Enter principal (p), rate of interest (r), number of years (n), and number of times interest is
compounded per year (q) for set 2: 9834
21
4
6
The amount for set 2 is: 22454.25
Enter principal (p), rate of interest (r), number of years (n), and number of times interest is
compounded per year (q) for set 3: 34
56
3
4
The amount for set 3 is: 163.81
Enter principal (p), rate of interest (r), number of years (n), and number of times interest is
compounded per year (q) for set 4: 2643
4
5
6
The amount for set 4 is: 3226.03
Enter principal (p), rate of interest (r), number of years (n), and number of times interest is
compounded per year (q) for set 5: 453535
45
56
58
The amount for set 5 is: 36190455769839944.00
Enter principal (p), rate of interest (r), number of years (n), and number of times interest is
compounded per year (q) for set 6: 545
435
56
56
The amount for set 6 is:
419446363842950700000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000.00
30
Enter principal (p), rate of interest (r), number of years (n), and number of times interest is
compounded per year (q) for set 7: 54656
3
34
43
The amount for set 7 is: 151517.84
Enter principal (p), rate of interest (r), number of years (n), and number of times interest is
compounded per year (q) for set 8: 656
54
4
4
The amount for set 8 is: 4975.51
Enter principal (p), rate of interest (r), number of years (n), and number of times interest is
compounded per year (q) for set 9:
546346
56
5
4
The amount for set 9 is: 7508700.72
Enter principal (p), rate of interest (r), number of years (n), and number of times interest is
compounded per year (q) for set 10: 777567
456
10
10
The amount for set 10 is: 16101833017536977000000.00
Q4
31
CODE
#include <stdio.h>
#include <math.h>
int main() {
double x,y, term, sum = 0;
int i;
y = pow(term,i);
sum = sum+0.5*y;
}
sum=sum+term;
return 0;
}
OUTPUT
Enter the value of x: 4
Sum of the first seven terms: 1.675
Q5
CODE
#include <stdio.h>
int main() {
int a, b, c;
return 0;
}
OUTPUT
345
5 12 13
6 8 10
7 24 25
8 15 17
9 12 15
10 24 26
12 16 20
15 20 25
18 24 30
20 21 29
Q6
CODE
#include <stdio.h>
int main() {
int population = 100000; // Initial population
double growth_rate = 0.10; // 10% growth rate
printf("Year\tPopulation\n");
printf("----\t----------\n");
33
for (int year = 0; year <= 9; year++) {
population += population * growth_rate;
printf("%d\t%lu\n", year, population);
}
return 0;
}
OUTPUT
Year Population
---- ----------
0 110000
1 121000
2 133100
3 146410
4 161051
5 177156
6 194871
7 214358
8 235793
9 259372
Q7
CODE
#include <stdio.h>
int main() {
int i, j, k, l;
int cube1, cube2;
34
cube1 = i * i * i + j * j * j;
if (cube1 == cube2) {
printf("Ramanujan number: %d\n", cube1);
}
}
}
}
}
return 0;
}
OUTPUT
Ramanujan number: 1729
Ramanujan number: 4104
Ramanujan number: 13832
Ramanujan number: 39312
Ramanujan number: 704977
Ramanujan number: 46683
Ramanujan number: 216027
Ramanujan number: 32832
Ramanujan number: 110656
Ramanujan number: 314496
Ramanujan number: 216125
Ramanujan number: 439101
Ramanujan number: 110808
Ramanujan number: 373464
Ramanujan number: 593047
Ramanujan number: 149389
Ramanujan number: 262656
Ramanujan number: 885248
Ramanujan number: 40033
35
Ramanujan number: 195841
Ramanujan number: 20683
Ramanujan number: 513000
Ramanujan number: 805688
Ramanujan number: 65728
Ramanujan number: 134379
Ramanujan number: 886464
Ramanujan number: 515375
Ramanujan number: 64232
Ramanujan number: 171288
Ramanujan number: 443889
Ramanujan number: 320264
Ramanujan number: 165464
Ramanujan number: 920673
Ramanujan number: 842751
Ramanujan number: 525824
Ramanujan number: 955016
Ramanujan number: 994688
Ramanujan number: 327763
Ramanujan number: 558441
Ramanujan number: 513856
Ramanujan number: 984067
Ramanujan number: 402597
Ramanujan number: 1016496
Ramanujan number: 1009736
Ramanujan number: 684019
Q8
CODE
36
#include <stdio.h>
int main() {
for (int hour = 0; hour < 24; hour++) {
if (hour == 0) {
printf("12 Midnight\n");
} else if (hour == 12) {
printf("12 Noon\n");
} else if (hour < 12) {
printf("%d : 00 AM\n", hour);
} else {
printf("%d : 00 PM\n", hour % 12);
}
}
return 0;
}
OUTPUT
12 Midnight
1 : 00 AM
2 : 00 AM
3 : 00 AM
4 : 00 AM
5 : 00 AM
6 : 00 AM
7 : 00 AM
8 : 00 AM
9 : 00 AM
10 : 00 AM
11 : 00 AM
12 Noon
1 : 00 PM
2 : 00 PM
3 : 00 PM
4 : 00 PM
5 : 00 PM
6 : 00 PM
37
7 : 00 PM
8 : 00 PM
9 : 00 PM
10 : 00 PM
11 : 00 PM
Q9
CODE
#include <stdio.h>
int main() {
int num = 1;
return 0;
}
38
OUTPUT
1
23
456
7 8 9 10
39
Chapter-7: Case Control Instruction
Q1
CODE
#include <stdio.h>
int main() {
int choice, num,check=1;
long long int fact =1;
while(1){
printf("Menu:\n");
printf("1. Factorial of a number\n");
printf("2. Prime or not\n");
printf("3. Odd or Even\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num == 0 || num == 1) {
return 1;
}
int i;
for(i=1; i<=num;i++)
{
fact = fact*i;
}
printf("Factorial of %d = %d\n", num, fact);
break;
case 2:
printf("Enter an integer: ");
40
scanf("%d", &num);
if (num <= 1) {
printf("%d is not a prime number.\n", num);
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
check=0;
}
}
if (check==1) {
printf("%d is a prime number.\n", num);
}
else
{
printf("%d is not a prime number.\n", num);
}
break;
case 3:
printf("Enter the integer: ");
scanf("%d", &num);
if(num%2==0)
{
printf("%d is an even number.\n", num);
}
else
{
printf("%d is an odd number.\n", num);
}
break;
case 4:
printf("Exiting the program. Goodbye!\n");
return 0;
break;
default:
printf("Invalid choice. Please select 1, 2, or 3.\n");
}
}
OUTPUT
Menu:
1. Factorial of a number
2. Prime or not
3. Odd or Even
4. Exit
Menu:
41
1. Factorial of a number
2. Prime or not
3. Odd or Even
4. Exit
Factorial of 19 = 109641728
Menu:
1. Factorial of a number
2. Prime or not
3. Odd or Even
4. Exit
Menu:
1. Factorial of a number
2. Prime or not
3. Odd or Even
4. Exit
45 is an odd number.
Menu:
1. Factorial of a number
2. Prime or not
3. Odd or Even
4. Exit
42
Chapter-8: Functions
Q1
CODE
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (leap_year(year)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
OUTPUT
Enter a year: 2024
43
2024 is a leap year.
Q2
CODE
#include <stdio.h>
void prime_factors(int n) {
while (n % 2 == 0) {
printf("%d ", 2);
n = n / 2;
}
while (n % i == 0) {
printf("%d ", i);
n = n / i;
}
}
if (n > 2) {
printf("%d ", n);
}
}
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
return 0;
}
OUTPUT
44
Enter a positive integer: 428
Prime factors of 428 are: 2 2 107
Chapter-9: Pointers
Q1
CODE
#include <stdio.h>
int main() {
int a, b, c;
return 0;
}
OUTPUT
Enter values for a, b, and c: 43
45
35
56
Before circular shift: a = 43, b = 35, c = 56
After circular shift: a = 56, b = 43, c = 35
Q2
CODE
#include <stdio.h>
int main() {
double kg, grams, tons, pounds;
return 0;
}
OUTPUT
46
Enter weight in kilograms: 60.20
Weight in grams: 60200.00 g
Weight in tons: 0.06 tons
Weight in pounds: 132.72 lbs
Q3
CODE
#include <stdio.h>
#include <math.h>
void distance(double x1, double y1, double x2, double y2, double
*dist) {
*dist = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
}
47
area_of_triangle(x, y, x3, y3, x1, y1, &area_PCA);
int main() {
double x1, y1, x2, y2, x3, y3;
double x, y;
int inside;
if (inside) {
printf("The point (%.2lf, %.2lf) lies inside the
triangle.\n", x, y);
} else {
printf("The point (%.2lf, %.2lf) does not lie inside the
triangle.\n", x, y);
}
return 0;
}
OUTPUT
Enter the coordinates of the first vertex (x1, y1): 6
7
Enter the coordinates of the second vertex (x2, y2): 2
48
1
Enter the coordinates of the third vertex (x3, y3): 3
4
Enter the coordinates of the point (x, y): 2
3
The point (2.00, 3.00) does not lie inside the triangle.
Chapter-10: Recursion
Q1
CODE
#include <stdio.h>
void binary_recursive(int n) {
if (n == 0) {
printf("0");
return;
}
binary_recursive(n / 2);
printf("%d", n % 2);
}
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
return 0;
49
}
OUTPUT
Enter a positive integer: 647
Binary equivalent : 01010000111
Q2
CODE
#include <stdio.h>
int sum_of_natural_numbers(int n) {
if(n==1)
{
return 1;
}
else{
return n+ sum_of_natural_numbers(n-1);
}
}
int main() {
int n = 50;
int sum = sum_of_natural_numbers(n);
return 0;
}
OUTPUT
50
The sum of the first 50 natural numbers is: 1275
Q3
CODE
#include <stdio.h>
if (n == 1) {
printf("Move disk 1 from peg %c to peg %c\n", from_peg,
to_peg);
return;
}
51
tower_of_hanoi(n - 1, from_peg, aux_peg, to_peg);
int main() {
int n ;
printf("Enter the number of disk in the first peg: ");
scanf("%d",&n);
return 0;
}
OUTPUT
Enter the number of disk in the first peg: 4
The sequence of moves to solve the Tower of Hanoi with 4 disks is:
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Move disk 3 from peg A to peg B
Move disk 1 from peg C to peg A
Move disk 2 from peg C to peg B
Move disk 1 from peg A to peg B
Move disk 4 from peg A to peg C
Move disk 1 from peg B to peg C
Move disk 2 from peg B to peg A
52
Move disk 1 from peg C to peg A
Move disk 3 from peg B to peg C
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Chapter-12: C Preprocessor
Q1
CODE
#include <stdio.h>
#define SQUARE(x) (x * x)
#define SUM(a, b) ((a) + (b))
int main() {
int x = 5;
int y = 10;
return 0;
}
Macro Expansion
# 6 "question1.cpp"
int main() {
int x = 5;
int y = 10;
53
printf("Sum of x and y: %d\n", ((x) + (y)));
return 0;
}
Q2
CODE
#include <stdio.h>
int main() {
int a = 10, b = 20, c = -30;
char upper = 'G';
54
printf("Biggest of %d, %d, and %d is %d\n", a, b, c,
BIGGEST_OF_THREE(a, b, ABSOLUTE_VALUE(c)));
return 0;
}
OUTPUT
Arithmetic mean of 10 and 20 is 15.00
Absolute value of -30 is 30
Lowercase of G is g
Biggest of 10, 20, and -30 is 30
Chapter-13: Arrays
Q1
CODE
#include <stdio.h>
int main() {
int numbers[25];
int positive_count = 0, negative_count = 0;
int even_count = 0, odd_count = 0;
printf("Enter 25 numbers:\n");
for (int i = 0; i < 25; i++) {
scanf("%d", &numbers[i]);
}
55
if (numbers[i] % 2 == 0) {
even_count++;
} else {
odd_count++;
}
}
return 0;
}
OUTPUT
4
5355
5345
-55
654353
65
3453534534
3453
65
343456
555
6
3456
536
5
3656
56
345
5
634
5346
57
635
557858
7878
76
Number of positive numbers: 24
Number of negative numbers: 1
Number of even numbers: 12
Number of odd numbers: 13
Q2
CODE
#include <stdio.h>
int main() {
int n;
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int is_symmetric = 1;
57
for (int i = 0; i < n / 2; i++) {
if (arr[i] != arr[n - 1 - i]) {
is_symmetric = 0;
break;
}
}
if (is_symmetric) {
printf("The array satisfies the condition.\n");
} else {
printf("The array does not satisfy the condition.\n");
}
return 0;
}
OUTPUT
Enter the number of elements in the array: 6
Enter 6 elements:
1
2
3
3
2
1
The array satisfies the condition.
Q3
CODE
#include <stdio.h>
int main() {
58
int arr[25];
int *ptr = arr;
printf("Enter 25 integers:\n");
for (int i = 0; i < 25; i++) {
scanf("%d", ptr + i);
}
return 0;
}
OUTPUT
Enter 25 integers:
3
43
53
35768
43546
34
65
35
4
343
656
59
45
3565
78
6
45
578
767567
66
4
6767
45
65
64
54
The smallest number in the array is: 3
Q4
60
CODE
#include <stdio.h>
int main() {
int arr[25];
printf("Enter 25 integers:\n");
for (int i = 0; i < 25; i++) {
scanf("%d", &arr[i]);
}
insertionSort(arr, 25);
printf("Sorted array:\n");
printArray(arr, 25);
return 0;
}
OUTPUT
Enter 25 integers:
654643
6757
36
5568
61
768
6757
798
5
65
6
68
5347
78
6
58
5
587856645
4769
86
767
97
57
8
66
8
Sorted array:
5 5 6 6 8 8 36 57 58 65 66 68 78 86 97 767 768 798 4769 5347 5568
6757 6757 654643 587856645
Q5
62
CODE
#include <stdio.h>
int main() {
int arr[10];
printf("Original array:\n");
for (int i = 0; i < 10; i++) {
arr[i] = i + 1;
printf("%d ", arr[i]);
}
printf("\n");
modify(arr, 10);
printf("Modified array:\n");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
63
OUTPUT
Original array:
1 2 3 4 5 6 7 8 9 10
Modified array:
3 6 9 12 15 18 21 24 27 30
Q6
CODE
#include <stdio.h>
#include <math.h>
int main() {
int data[] = {-6, -12, 8, 13, 11, 6, 7, 2, -6, -9, -10, 11, 10, 9,
2};
int size = sizeof(data) / sizeof(data[0]);
64
double mean = calculateMean(data, size);
return 0;
}
OUTPUT
Mean: 2.40
Standard Deviation: 8.40
Q7
CODE
#include <stdio.h>
#include <math.h>
65
int main() {
return 0;
}
OUTPUT
Plot No. 1: Area = 3908.71 square units
Plot No. 2: Area = 5585.06 square units
Plot No. 3: Area = 6151.55 square units
Plot No. 4: Area = 2750.04 square units
Plot No. 5: Area = 5090.65 square units
Plot No. 6: Area = 8838.16 square units
Q8
66
CODE
#include <stdio.h>
#include <math.h>
int main() {
double x[] = {34.22,39.87,41.85,43.23,40.06,53.29,53.29,54.14,
49.12, 40.71, 55.15};
double y[] = {102.43,100.93,97.43,97.81,98.32,98.32,100.07,97.08,
91.55, 94.89, 94.65};
int n = 4;
67
double numerator = n * sum_xy - sum_x * sum_y;
double denominator = sqrt((n * sum_x_squared - sum_x * sum_x) * (n
* sum_y_squared - sum_y * sum_y));
double r = numerator / denominator;
return 0;
}
OUTPUT
Correlation coefficient (r) = -0.9108
Q9
CODE
#include <stdio.h>
#include <math.h>
int main() {
int numPoints = 10;
double x[numPoints], y[numPoints];
double totalDistance = 0.0;
68
printf("Total distance between the first and last point: %.2lf\n",
totalDistance);
return 0;
}
OUTPUT
Enter the X and Y coordinates of 10 points:
Point 1 (X Y): 4
5
Point 2 (X Y): 2
3
Point 3 (X Y): 5
8
Point 4 (X Y): 4
9
Point 5 (X Y): 23
67
Point 6 (X Y): 1
2
Point 7 (X Y): 0
1
Point 8 (X Y): 8
8
Point 9 (X Y): 3
3
Point 10 (X Y): 5
5
69
Total distance between the first and last point: 161.67
Q10
CODE
#include <stdio.h>
#include <stdbool.h>
#define MAX_SIZE 10
char dequeue[MAX_SIZE];
int left = -1;
int right = -1;
bool isEmpty() {
return (left == -1 && right == -1);
}
bool isFull() {
return (right == MAX_SIZE - 1);
}
char retrieveLeft() {
if (isEmpty()) {
printf("Dequeue is empty. Cannot retrieve.\n");
return '\0';
}
char item = dequeue[left];
if (left == right) {
left = right = -1;
} else {
left++;
}
return item;
}
char retrieveRight() {
if (isEmpty()) {
printf("Dequeue is empty. Cannot retrieve.\n");
return '\0';
}
char item = dequeue[right];
if (left == right) {
left = right = -1;
} else {
right--;
}
return item;
}
int main() {
insertLeft('A');
insertRight('B');
insertLeft('C');
insertRight('D');
OUTPUT
Retrieve from left: C
Retrieve from right: D
CODE
#include <stdio.h>
int main() {
int threed[3][2][3] = {
{
{1, 2, 3},
{4, 5, 6}
},
{
{7, 8, 9},
{10, 11, 12}
},
{
72
{13, 14, 15},
{16, 17, 18}
}
};
return 0;
}
OUTPUT
First element: 1
Last element: 18
Row 1: 1 2 3
Row 2: 4 5 6
2D Array 2:
Row 1: 7 8 9
Row 2: 10 11 12
73
2D Array 3:
Row 1: 13 14 15
Row 2: 16 17 18
Q2
CODE
#include <stdio.h>
int main() {
int size;
int matrix[10][10];
if (isSymmetric(matrix, size)) {
printf("The matrix is symmetric.\n");
} else {
printf("The matrix is not symmetric.\n");
}
74
return 0;
}
OUTPUT
Enter the elements of the matrix:
1
1
1
1
1
1
1
1
1
The matrix is symmetric.
Q3
CODE
#include <stdio.h>
int main() {
int matrix1[6][6] = {
{1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24},
{25, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36}
};
int matrix2[6][6] = {
{36, 35, 34, 33, 32, 31},
75
{30, 29, 28, 27, 26, 25},
{24, 23, 22, 21, 20, 19},
{18, 17, 16, 15, 14, 13},
{12, 11, 10, 9, 8, 7},
{6, 5, 4, 3, 2, 1}
};
int result[6][6];
return 0;
}
OUTPUT
The resulting matrix is:
37 37 37 37 37 37
37 37 37 37 37 37
37 37 37 37 37 37
37 37 37 37 37 37
37 37 37 37 37 37
37 37 37 37 37 37
Q4
76
CODE
#include <stdio.h>
int main() {
int matrix1[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int matrix2[3][3] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int result[3][3];
;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
;
printf("The resulting matrix is:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
77
}
OUTPUT
The resulting matrix is:
30 24 18
84 69 54
138 114 90
Q5
CODE
#include <stdio.h>
arr[size - 2] = temp1;
arr[size - 1] = temp2;
}
78
int main() {
int matrix[4][5] = {
{15, 30, 28, 19, 61},
{22, 33, 44, 55, 66},
{77, 88, 99, 11, 21},
{31, 41, 51, 61, 71}
};
printf("Original matrix:\n");
printMatrix(matrix);
return 0;
}
OUTPUT
Original matrix:
15 30 28 19 61
22 33 44 55 66
77 88 99 11 21
31 41 51 61 71
79
80