1. C program to perform all arithemetic operations
include
int main(void) {
double num1, num2;
char op;
printf("Enter an operator (+,
oD
scanf("%c", &op);
printf("Enter two numbers: ");
scanf ("8 9, num, &num2);
if (of weit
printf("%.1If + 96.11F= 96.2If\n", numa, num2, num’ +num2);
Jelse if (op ==") {
printf("96. if - %.11f = %.2If\n", num4, num2, num - num2);
Jelse if (op rf
Printf("96. lif * 6.11 = 96.LIF\n", numa, num2, num * num2);
Vt
printf("%.1If / %.11F = %.1IF\n", num, num2, num / num2);
Jelse if (op.
Jelse{
printf("Error: Invalid operator\n");
}
return 0;
)
OUTPUT
Enter an operator (+, -,*,/):+
Enter two numbers: 10
20
10.0 +20.0=30.0
2. C program to find area of a triangle if base and height are given.\clude
int main()
{
float base, height, area;
printf("Enter base of the triangle: ");
scanf("%"", &ibase);
printi("Enter height of the triangle: ");
scanf("%6f", &height};
area = (base * height) / 2;
printf("Area of the triangl
return 0;
%.2f sq. units", area);
}
ourpuT
Enter base of the triangle: 20,
Enter height of the triangle: 15
Area of the triangle = 150.00 sq. units
program to find all angles of triangle if two angles are given.
include
int main(void) ¢
int angle1, angle2, angle3;
printf("Enter the first angle of the triangle:
scanf(""éd", &anglel);
printf(“Enter the second angle ofthe triangle: ")
scanf("¥éd", angle2);
angle3 = 180 - {angle + angle2);
printf(*The third angle of the triangle is: %d\n", angle3);
return 0;
}
outeut
Enter the first angle of the triangle: 70
Enter the second angle of the triangle: 65
‘The third angle of the triangle is: 45
C program to convert days into years, weeks and days.
include
int main()
{int days, years, weeks;
printf("Enter the number of days: ");
scanf("7éd", &days);
years = days / 365;
weeks = (days % 365) /7;
days = (days % 365) % 7;
printf("Years: %d\n", years);
printf("Weeks; %d\n", weeks);
printi("Days: %d\n", days);
return 0;
}
ourPut
Enter the number of days: 816
Years: 2
Weeks: 12
Days: 2
5. C program to find power and square root of any number.
finclude
include
int main()
{
double num, power, square_root;
printf("Enter a number: ");
scanf("%lf", &num);
power = pow(num, 2);
‘square_root = sqrt(num);
printf("Square: %.2If\n", power);
printf("Square root: %.2If\n", square_root);
return 0;
)
output
Enter a number: 49
Square: 2401.00
Square root: 7.00
6. C program to calculate total ,average, percentage and grades of five subject.\clude
int maint) {
float marks(5];
float total = 0;
float average;
float percentage;
inti;
for (i= 0; 1< 5; i+) {
printf("Enter marks for subject 9d", 141);
scanf("9f", &marks[i));
total += marks|i];
}
average = total /5;
percentage = (total / 500) * 100;
printf("Total marks: %.2A\n", total);
printf("Average marks: %.2f\n", average);
printf(*Percentage: %.2f\n", percentage);
if (percentage >= 90) {
printf("Grade: At\n");
J else if (percentage >= 80) {
printf("Grade: A\n");
J else if (percentage >= 70) {
printf("Grade: B\n");
} elseif (percentage >= 60) {
printf("Grade: B\n"
J else if (percentage >= 50) {
printf("Grade: C+\n");
J elseif (percentage >= 40) {
printf("Grade: C\n"
}else{
printf("Grade: Dn
return 0;
}
ourput
Enter marks for subject 1: 90
Enter marks for subject 2: 85.
Enter marks for subject 3: 95.
Enter marks for subject 4: 80
Enter marks for subject 5: 78
Total marks: 428.00
Average marks: 85.60
Percentage: 85.607
Grade: A
program to check least significant Bit (LSB) and MSB of a number using bitwise operator.
include
int main() {
int num;
printf("Enter a number:
scanf"%6d", &num};
if (num & 3) {
printf("LSB is set (1).\n");
Jelse {
printf("LSB is not set (0).\n");
3
if (num & (1 <« (sizeoffint) * 8 -1))){
printf("MsB is set (1).\n");
Jelse {
printf("MSB is not set (0).\n");
}
return 0;
}
output
Enter a number: 1
LSBis set (2).
MB is not set (0).
program to swap two number using 3” variable and without 3” variable.
ude
int main() {
int a, b, temp;
printf("Enter two numbers:
scanf('%d%d", &a, &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
temp =a;
a=b;
b= temp;
printf("After swapping: a = %d, b = %d\n", a,b);
return 0;
}
output
Enter two numbers: 5
10
Before swapping: a=5, b= 10
After swapping: a= 10, b =59%.
10.
a
C program to find maximum between three number using conditional operator and ternary
operator.
include
int main {
inta,b,¢;
printf("Enter three numbers: ")
scanf("%d%d%d", &a, &b, &c);
intmax=a>b?(a>c?a:c):(b>¢?b:c);
printf("Maximum: d\n", max};
return 0;
)
OUTPUT
Enter three numbers: 20
50
10
Maximum: 50
C program to check alphabet, digit or special character using conditional operator.
Hinclude
int main()
{
char ch;
printi("Enter a character: ");
scanf"%6c", &ch);
(ch >= "a! && ch <=) || (ch >='A' &B& ch <='Z') ? printf("%4c' is an alphabet.\n", ch)
(ch >='0' && ch <= '9')? printf{"%c' is a digit.\n", ch) =
printf("%c' is a special character.\n", ch);
return 0;
}
ouTPUT
Enter a character: A
"R’isan alphabet
C program to calculate total electricity bill.
include
int main()
{
int units;
float bill;
printi("Enter the number of units consumed.
scanf("¥éd", &units);
if (units <= 50)
bill = units * 0.50;
else if (units <= 150)bill = 25 + (units - 50) * 0.75;
else if (units <= 250)
bill = 100 + (units - 150) * 1.20;
else
bill = 220 + (units - 250) * 1.50;
printi("Total electricity bill: $%.2f\n", bill)
return 0;
)
output
Enter the number of units consumed: 250
Total electricity bill: $220.00
12. C program to create simple calculator and days of week using switch case.
#include
int main() {
int nur, num2, result;
char operator;
int day;
printf("Enter an operator (+, ~,*,
scanf{"%c", operator);
printf("Enter two operands: ")
scanf("%d %d", &rumt, &rum2);
switch (operator) {
case'+'
result = num + num2;
break;
case
result = numt - num2;
break;
case":
result = num * num2;
break;
case'/'
result = numt / num2;
break;
default:
printf("Error! operator is not correct");
return 0;
}
printf("%.1d %c %.1d = 9.14", numt, operator, num2, result);
printf(*\nEnter a number between 1 and 7: ");
scanf("%d", &day);
switch (day) {case
printf(" Monday");
break;
case2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf('Saturday");
break;
case 7:
printf('Sunday");
break;
default:
printf("Error! Invalid number entered");
}
return 0;
)
13, C program to check vowel or consonant using switch case.
finclude
int main() (
char ch;
printf("Enter a character: ");
scanfl"%6c", &ch);
switch (ch) (
case ‘a’:
case'e
case
case 'o':
case'u'
case'A'
case 'E'
case '':
casecase'U:
printf("%6c is a vowel.", ch);
break;
default:
printf("%6c is a consonant.", ch);
}
return 0;
)
ourput
Enter a character: g
gis a consonant,
14, C program to check positive, negative or zero using switch case.
#include
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
switch (num > 0) {
case 1
printf("%éd is positive.", nur);
break;
case 0
switch (num ==0){
case 1
printf("%éd is zero.", num);
break;
case 0:
printf{"séd is negative.", num};
}
break;
3
return O;
}
ourput
Enter a number: 25
25 is positive.
15. C program to check weather a triangl
include
equilateral, isosceles or scalene.
int maint) {
int sided, side2, side3;
printf("Enter the sides of the triangle: "};
scanf("%d %d %d", Bsidel, &side2, &side3);switch (side:
case 1:
switch (side2 == side3) (
case 1:
printf("The triangle is equilateral");
break;
case 0:
print("The triangle is isosceles.");
}
break;
case 0:
switch (side2 == side3) {
case 1:
printf("The triangle is isosceles.");
break;
case 0:
printf("The triangle is scalene.");
,
}
return 0;
}
oureut
Enter the sides of the triangle: 45,
60
60
The triangle is isosceles.
16. C program to print all natural number and sum of it from 1 to n.
ffinclude
int main(void) {
int n,i, sum
printf("Enter a positive integer:
scanf("96d", &n);
for (i= 4;i<=n; +4) (
printf("%6d", i);
sum +=,
}
printf("\nSum = 9d", surn);
return 0;
}
oureut
Enter a positive integer: 1007.
18.
123456789 101112 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 37 38 39.40 41.42.43 44.45 46 47 48 49 50 51 52 53 54 55 5657 58 59 60 61 62 63 64 65 66
67 68 69 7071 72.73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
98 99 100
Sum = 5050
€ program to print all even number and sum of it from 1 ton.
Jude
int main(void) {
int n, i, sum =0;
printf("Enter a positive integer: ");
scanf{"9éd", &n);
for(i=2;i<= M51
printf("%d", i);
sum +i;
}
printf("\nsum = %d", sum);
return 0;
y
ourrut
Enter @ positive integer: 60
2.468 10 12 14 16 1820 22 24 26 28 30 32 34 36 38.40 42 44 46 48 50 52 545658 60
Sum = 930
C program to print multiplication table of a number.
#include
int main(void) {
int n, i;
printf("Enter a number: ");
scanf("9éd", &n);
for (i= 151 <=10; +41) {
printi("%6d x %d = %d\n", n, isn * is
1
return 0;
}
outeut
Enter a number: 17
a7x1=17
17x2=34
17x3=51
17x4=68
17x5=85
17 x6 = 102
17 x7=119
ay17x8= 136
17x9=153
17x10=170
C program to calculate factorial of a number.
include
19.
int main(void) {
intn, i fact= 1;
printf(*Enter a positive integer: ");
scanf(""éd", &n);
for (i= 1; 1<= 1; +i) (
fact *=
}
print ("Factorial of éd = 96d", n, fact);
return 0;
}
ourput
Enter @ positive integer: 5
Factorial of 5 = 120
20. € program to check weather a number
include
palindrome or not.
int main(void) {
int n, reversed = 0, remainder, original;
printf(“Enter an integer: ");
scanf("%d", &n);
original
while (n I= 0) {
remainder =n % 10;
reversed = reversed * 10 + remainder;
if (original == reversed) {
printf("%d is @ palindrome." original);
Jelse {
printf("%6d is not a palindrome.", original);
}
return 0;
?
output
Enter an integer: 121
121 is a palindrome.
21. C program to count frequency of digits in a given number.
Hinclude int main(void) {
int n, digit, count{10] = (0);
printi("Enter a number: ");
scanf("96d", &n);
while (n> 0) {
digit = n % 10;
count{di
n/=10;
}
printf(*Digit-\S\t");
for (int i= 0; i< 10; i++) (
printf("s6d\e", i)
1
printf("\nFrequency:\t");
for (ir i< 10; i++) {
printf("séd\t", countli));
}
return 0;
}
ourpuT
Enter a number: 987456321
Digit: o 123 45 67 8 9
Frequency. © 2 2 21 1 1 1 1 2 21
2. C program to find HCF(GCD) and LCM of two numbers.
include
int main(void) ¢
int n1, n2, i, ged, len;
printf("Enter two positive integers: ");
scanf("sad %d", &in1, &in2);
for (I= 1; 1<= m1 &Bi<= m2; +4i){
0&&n2%i==0)(
}
}
lem = (ni * n2) / ged;
printf("GCD of %d and %d is %d\n", m1, n2, gcd);
printf("LCM of 94d and 9d is %d\n", m1, n2, lem);
return 0;
}
ourpuT
Enter two positive integers: 16
nGCD of 16 and 72 is 8
LCM of 16 and 72 is 144
23. C program to print all prime number between 1 ton.
include
include
int main(void) {
int n, ij
bool is_prime;
printf(“Enter a positive integer:
scanf("9éd", &n);
printf(*All prime numbers between 1 and %d are:\n", n);
is_prime = false;
break;
,
}
if (is_prime) {
printi("%d
}
)
return 0;
}
ourPur
Enter a positive integer: 591
All prime numbers between 1 and 591 are:
2357 1113.17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127
4131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241,
251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379
383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491.499 503 509
521 523 541 547 557 563 569 571 577 587
24, C program to print all strong number between 1 ton.
Sinclude
include
int main(void) {
int n, ij last_digit, sum;
bool is_strong;
printf(“Enter a positive integer: ");
scanf("sed", &n);
print#(*All strong numbers between 1 and %d are:\n", n};is_strong = true;
while (j>0){
last_digit =) % 10;
i
int fact =1;
for (int k= 1; k <= last_digit; +k) {
fact *=k;
}
sum += facts
}
if (sum ==i)
printf("96d", i
}
}
return 0;
)
output
Enter a positive integer: 789
All strong numbers between 1 and 789 are:
12145
25. C program to print Fibonacci series upto n terms.
include
int main(void) {
int n, i, first = 0, second = 1, next;
printf("Enter the number of terms:
scanf("%d", &n);
printf(*Fibonacci series: ")
for (i= 1; i<=n; ++i) {
printi("%d, , first);
rst + second;
second = next;
}
return 0;
}
ourpuT
Enter the number of terms: 20
Fibonacci series: 0, 1, 1, 2,3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181,26. C program to print Armstrong number from 1 ton and check a given number is armstrong
number or not.
include
include
int main(void) {
int n, i, num, last_
bool is_armstrong;
printf(“Enter a positive integer: ");
scanf("%éd", &n);
printf("All Armstrong numbers between 1 and %d are:\n", n);
for (i= 1;1<= 1; +4i)(
igit, digits, sum, original;
sum
num
original = num;
digits = (int)log1O(num) + 1;
while (num > 0) {
last_digit = num % 10;
ow(last_digit, digits);
if original == sum) {
printi("%d ", original);
}
}
printf("\n€nter a number to check if it is an Armstrong number:
scanf("96d", &num);
original = num;
sum =0;
digits = (int)logiO(num) + 1;
while (num > 0) {
last_digit = num % 10;
sum += powllast_digit, digits);
num /= 10;
}
{s_armstrong = (original
printf("94d is", original);
if (is_armstrong) {
printf("an Armstrong aumber.");
J else {
printf(*not an Armstrong number.
}
return 0;
sum);27.
}
ourpuT
Enter a positive integer: 1000
All Armstrong numbers between 1 and 1000 are:
123456789 153 370371 407
Enter a number to check fit is an Armstrong number: 152
152 is not an Armstrong number.
C program to print all perfect number between 1 to n and check a given number is perfect
number or not.
include
int main(void) {
int n, i,j, sum, num;
bool is_perfect;
printf("Enter a positive integer: ");
scanf("s6d", &n);
printf("All perfect numbers between 1 and %d are:\n", n);
for (i= 2; 1<=n; +41) (
printf ("6d ", i);
}
d
printf("\nEnter a number to check
scanf("séd", &num);
sum = 0;
isa perfect number: ");
is_perfect = (sum
printf("s%6d is", num);
if (is_perfect) {
rintf("a perfect number.");
Jelse {
printf("not a perfect number.");
jum);}
return 0;
)
output
Enter a positive integer: 999
All perfect numbers between 1 and 999 are:
628.496
28. C program to find power of any number using for loop.
include
int main(void) {
int base, exponent, result = 1;
printi("Enter base: ");
scanf("sid", base);
printf(“Enter exponent: ");
scanf{"%éd", &exponent);
for (inti= 1; i<= exponent; +4i){
result *= base;
}
printf(*séd to the power of %d is éd\n", base, exponent, result};
return 0;
}
output
Enter base: 10
Enter exponent: 3
10 to the power of 3is 1000
29. C program to print ASCII value s of all characters.
Jude
int main(void) {
printf(*ASCll values of all characters:\n");
for (int i= 0;i< 256; ++i) {
printi("%d: %6e\n", is
)
return 0;
)
ouput
ASCII values of all characters:
0:
1:0
2®
ae
aeof
oe
10:
Ine
12:9
13:
14
15:
16
7:
18:
19,
20:
21:
22
23:
24:
25:
26:
27;
28:
29:
30:
31
32:
33:1
Cayvoxu
arg tvesr yas
35:
36:$
37:%
38: &
39:"
40: (
41:)
42:*
43:+
4a: ,45:
46:
47:
48,
49;
50:
51
52:
53:
54,
55:
56.
57:
58:
59:
60:
61:
62:
63:
6a
65:
66:
67:
68:
69:
70:
n
72:
73:
74:
75:
76:
7:
78:
79:
80:
a1:
82:
83:
8a,
a5:
SCAM DD POZEH KR —~THTMOOSSHVY87: W
88:X
89:1
90:2
onl
92:\
93:]
9a:
98:_
96°
97a
98:b
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109
110:
SEE
12:
113:
114:
11s:
116:
17:
118:
119:
120:
121:
122:
123:
124: |
125:}
126:
127:0
28:6
NER gear esas es gee129)
130,
131:
132,
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,
160:
PRM OGCOSSOCOORR MED ~
161:
162:
163)
164,
165:
166:
167:
168:
169:
170:kise ses) esearch bo
rs213: F
214:
215-4
216:+
274
218: f
219:
220:
rau:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246: +
2a7=
248:°
249:
250:
251:V
252:"
253:
2540
59S EOD0E47F OM 558 Sem
2
f
J255:
30. C program to print pascal triangle upto n rows.
include
int main()
{
int rows, coef =1, space, i,j;
printi("Enter number of rows:
scanf("%d", Brows);
forliz0; icrows; i++)
{
for(space=1; space <= rows-I; space++)
printf(" ");
for(i=0; | <= 1,44)
{
if
coe
else
coef = coef*(-j+1)/i;
printf("%4d", coef);
,
printf("\n");
}
return 0;
}
ouTpUT
Enter number of rows: 10
1
aa
124
1331
14641
15101051
1615201561
172139352171
1-8 28 56 7056 28 8 1
1 9 36 84126126 84 36 9 1
31. C program to find sum of all elements of array.
include
int main()
{int arr{100}, size, , sum =
printf("Enter size of the array: ");
scanf("%d", &size);
printf("Enter elements of the array: ");
for(i=0; i
int main()
{
int source[100}, target[100], size, i;
printf("Enter size of the array: "};
scanf("%d", &size);
printf("Enter elements of the source array: ");
for(i=0; i
int main()
{
int arr[100], size, i, pos, element;
printf("Enter size of the array: ");
scanf("%d", &size);
printf("Enter elements of the array: ");
for(i=0; i=pos; -)
{
arrlit4] = arr;
}
arr[pos] = element;
printf(*Array after insertion: "}
for(i=0; i<=size; i++)
{
printt("96d", arr);
}
return 0;ourrut
Enter size of the array: 5
Enter elements of the array: 4
9
8
2
6
Enter the position where you want to insert an element: 2
Enter the element you want to insert: 47
Array after insertion: 49.47 82.6
|. C program to delete an element in array at specified position.
include
int main()
{
int arr{100), size, i, pos;
printf("Enter size of the array: "
scanf("%d", &size);
printi("Enter elements of the array: ");
forti=0; icsize; i++)
{
scanf{"96d", &arrli);
}
printf("Enter the position of the element you want to delete:
scanf("%d", &p03);
forli=pos-1; icsize-1; i++)
{
arrli] = arrli+t};
}
printf("Array after deleting an element: ")
forli-O; icsize-1; i++)
{
printf("6d ", arrlil);
}
return 0;
}
ourrut
Enter size of the array: 4
Enter elements of the array: 3
7
1
9
Enter the position of the element you want to delete: 3Array after deleting an element: 379
135. C program to search element in array using linear search.
include
Hdefine ARRAY_SIZE 10
int main(void) {
int array[ARRAY_SIZE] = (1,2, 3,4, 5,6,7,8,9, 10};
int search_key = 5;
int found = 0;
int;
for (i= 0; i< ARRAY_SIZE; i++) {
if (arrayfil == search_key) {
found = 1;
break;
}
}
if Found) {
printf("The search key was found at index %d\n", I];
else {
printf("The search key was not found in the array\n’
)
return 0;
)
ourPuT
‘The search key was found at index 4
36. C program to find second largest number and sorting using bubble sort in an array.
include
define ARRAY SIZE 10
void bubble_sort(int array[], int size) {
inti
for (i= 0;i< size 1; +4) (
for (j= 0; j arraylj + 1)){
int temp =arrayll;
arraylj] = arrayl)+ 11;
arrayl) +1] =temp;
}
}
)
}
int main(void) {
Int array[ARRAY_SIZE] = (9, 8, 7,6,5, 4,3, 2,1, 0};
int second_largest;bubble_sort{array, ARRAY. SIZE);
second_largest = array[ARRAY_SIZE - 2];
printf(*The second largest number is 94d\n", second_largest);
return 0;
)
output
The second largest number is 8
37. C program to count total number of duplicate element in an array.
include
fHdefine ARRAY_SIZE 10
int main(void) {
int arraylARRAY_SIZE] = (1, 2, 3,3, 4,4, 4,5,5, 5}
inti j
int count = 0;
for (i= 0;i-< ARRAY_SIZE; it+) {
for (j= i+ 1; j
Hidefine ROWS 3
fidefine COLUMNS 3
int main(void) {
int matrix{ROWS][COLUMNS] = {(1, 2, 3), (4 5, 6), (7,8, 9;
int scalar = 2;
inti:
for (i= 0;1< ROWS; i++) (
for (| = 0; | < COLUMNS; j++) {
matrixifi] *= scalar;
}
}
for (i= 0;1< ROWS; ++) (
for (|= 0; j< COLUMNS; +) {39.
printf("%d", matrixfi]G);
}
printf("\n");
}
return 0;
}
ouTPuT
246
81012
141618
€ program to find sum of main diagonal element of a matrix.
include
define ROWS 3
define COLUMNS 3
int main(void) {
int matrix{ROWS][COLUMNS] = {(1, 2, 3), (4, 5, 6}. (7,8, 9);
inti;
int sum = 0;
for (i= 0; i< ROWS; i++) {
sum += matrix{iJ[il;
}
printf("The sum of the main diagonal elements is %d\n", sum);
return 0;
d
ourPuT
The sum of the main diagonal elements is 15
€ program to check sparse and transpose matrix.
include
define ROWS S
define COLUMNS 5
int main(void) {
int matrix{ROWS][COLUMNS] = {(0, 1, 0, 0, 0},
{0, 0, 2, 0,0),
{0, 0, 0, 3, 0},
{0, 0,0, 0, 4},
{0, 0, 0, 0, 0};
inti, is
int is_sparse = 1;
int is_transpose = 1;
for (i= 0;1< ROWS; i++) (
for (|= 0; j< COLUMNS; +) {
if (matrintU] != 0) ¢for (i= 0;i< ROWS; t+) {
for (j= 0; j < COLUMNS; j++) {
if (matrx(] i} != matrix) (
is transpose
break;
}
}
}
if (is_sparse && is transpose) {
printi("The matrix is both sparse and transpose\n");
Jelse {
printi("The matrix is not both sparse and transpose\n");
}
return 0;
)
output
The matrix is not both sparse and transpose
41. C program to check weather a matrix is identify matrix or not.
include
Hidefine ROWS 3
fidefine COLUMNS 3
int main(void) {
int matrix{ROWS][COLUMNS] = {{1, 0,0},
{0,1,0),
{0,0,195
0; i< ROWS; i++) (
),j < COLUMNS; j++) {
IF (1 == j && matrix] He 1) {
is_identity = 0;
break;
elseif (/1=] && matrix] I= 0) {
is_identity = 0;
break;
}
}42.
}
if (is_identity) {
printf("The matrix is an identity matrix\a");
Jelse {
printi("The matrix is nat an identity matri\n");
)
return 0;
)
ourPut
The matrixis an identity matrix
C program to merge two sorted array in ascending order.
finclude
fidefine ARRAY. SIZE_15
define ARRAY SIZE 28
define MERGED_ARRAY SIZE (ARRAY SIZE_1 + ARRAY_SIZE_2)
int main(void) {
Int array_A[ARRAY_SIZE_1] = (1, 3,5,7,9};
int array_2[ARRAY_SIZE_2] = {2, 4, 6,8, 10, 12, 14, 16};
int merged_array[MERGED_ARRAY SIZE];
inti =0,j=0, k=0;
while (i< ARRAY SIZE_1 &8 j< ARRAY SIZE_2){
if (erray_A{i] < array_2U) {
merged_array{k] = array_1{i)
Jelse {
merged_arraylk] = array_2Uj}
is
}
kes;
}
while (i< ARRAY_SIZE_1) {
merged_arraylk] = array_1(i];
is
ket;
)
while (< ARRAY_SIZE_2){
merged_arraylk] = array_2{j];
ines
kets
}
for (i= 0;i< MERGED_ARRAY_SIZE; i++) {
printi("%d", merged_arraylills1
printf("\n");
return 0;
?
output
12345678910121416
43. C program to perform all operation of string,
finclude
#include
void main(){
Char string1{25] string2[25];
int
Clrscrt);
printf("***** performing string length ******\n");
printf(“enter only one string \n");
scanf("%s",string1);
trien(string!);
printf("the string length is %d\n\n".1);
printf("**** performing string concatenation ****\n");
printf(*enter two strings\n");
scanf{"94s%s" string! string2);
printf("the concatenated string is %s\n\n",streat(string!,string2));
printi("***** performing string compare *****\n");
printf("enter two strings \n");
scanf{"94s%s" stringt,string2);
if{stremp(string1, string?)
printf("strings are equal\n"
0)
else
printf("strings are not equal\n");
printf("*** performing string copy ****\n"};
printf("enter the two strings\
scanf("%6d%4¢" stringl,string2);
printf("the first string is 9s and second string is %s\n",stringl,string2);
strepy(string1,string2);
printf("the first string is 9s and second string is %s\n",stringl,string2);
return 0;
y
44, C program to check weather a string is palindrome or not without compare function of string.
include
include int main()
{
cchar str{] = {“abbba" };
int |=0;
int h= strlen(str) - 1;
while (h > I){
if (str{l+4] #= strth]){
printf("%s is not a palindrome\n", str);
return 0;
}
printi("%s is a palindrome\n", str};
return 0;
}
output
abba is a palindrome
45. C program to count frequency of each character ina string.
include
int main {
char str[1000}, ch;
int count = 0;
printi("Enter a string: "};
fgets(str, sizeof(str), stdin);
printi("Enter a character to find its frequency: ";
seanf("%6c", &ch};
for (int i= 0; st[i] != \0'; +48) {
if (ch == sti)
+4count;
3
printf("Frequency of %c= Sd", ch, count);
return 0;
}
outeut
Enter a string: My name is Bhuvan Agrawal of section s studing
Enter a character to find its frequency: 5
Frequency of s= 5,
46. C program to find diameter, circumference and area of circle using functions.
include
ffinclude
double getDiameter(double radius);
in GLA University
double getCircumference(double radius);47.
double getArea(double radius);
int main()
{
float radius, dia circ, area;
printf("Enter radius of circle: ");
scanf("%6"", &radius);
dia = getDiameter(radius);
circ = getCircumferencefradius);
area = getArea(radius);
printf("Diameter of the circle = %.2f units\n", dia);
printf("Circumference of the circle =%.2f units\n", circ);
printf(*Area of the circle = %.2f sq. units", area);
return 0;
)
double getDiameter(double radius)
{
return (2 * radius);
y
double getCircumference(double radius)
{
return (2 * M_PL* radius);
?
double getArea(double radius)
{
return (M_PI* radius * radius);
)
output
Enter radius of circle: 21
Diameter of the circle = 42.00 units
Circumference of the circle = 131.95 units
Area of the circle = 1385.44 sq. units
program to check prime, Armstrong and perfect numbers using functions.
include
finclude
int isPrime(int num);
int isArmstronglint num);
int isPerfect{int num;
int main()
{
int num;
printf("Enter any number: ");
scanf("%d", &num);if{isPrime(num))
{
printf("éd is Prime number\n", num);
}
else
{
printf("%6d is not Prime number.\n", num);
}
iflisarmstrong(num))
{
printf("%d is Armstrong number.\n", num);
3
else
{
printf("éd is not Armstrong number.\n", num);
}
iflisPerfect(num))
{
printf("96d is Perfect number.\n", num);
}
else
{
printi("96d is not Perfect number.\n", num);
1
return 0;
}
int isPrime(int num)
{
inti;
forli=2;
{
if{nums%i
{
return 0;
}
1
return 1;
}
int isArmstrong(int num)
{
int lastDigit, sum, originalNum, digits;
yum/2; i++)
9)sum = 0;
originalNum = num;
digits = (int) logiO(num) + 1;
while(num > 0)
lastDigit = num % 10;
sum.
num = num / 10;
}
return (originalNum
}
int isPerfect(int num)
{
int, sum, n;
sum
sum);
n=num;
for(i=1; icn; i++)
{
iffns6i
return (num
}
ourpuT
Enter any number: 7
Tis Prime number.
7 is Armstrong number.
Tis not Perfect number.
48. C program to add two numbers using pointers.
include
sum);
int main(){
int first, second, *p, *4, sum;
printf("Enter two integers to add\n");
scanf"%d%d!, Bfirst, &second);
p= &first;
a= &second;
sum="p+"q;
printf("Sum of entered numbers = %d\n",sum);,
return;
1um + round(pow(lastDigit, digits);ourrut
Enter two integers to add
5
10
Sum of entered numbers = 15
C program to swap 2 numbers using call by value and call by refrence.
ude
void swap(int*, int*);
int maint)
{
int, ys
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = Y%d\ny = 96d\n", xv}
swapl8x, &y);
printf("After Swapping\nx = %d\ny =
return 0;
y
void swap(int *a, int *b)
49.
d\n", x,
ourput
Enter the value of xand y
50
100
Before Swapping
x=50
y=100
After Swapping
x=100
y=50
50. C program to copy an array to another array and reverse an array using pointers.
include
fHdefine MAX_SIZE 100
void printArr(int *arr, int size);
int main()
{
int arr{MAX_SIZE);int size;
int “left = arr;
int *right;
printf("Enter size of array: ");
scanf("%d", &size);
right = &arr{size-1);
printf("Enter elements in array: ");
while(left <= right)
{
scanf{"96d", left#+);
}
printf("\nArray before reverse: ");
printArr(arr, size);
left = arr;
while(left < right)
left *= right;
right = *left;
left $= right;
lefts;
right~
printf("\nArray after reverse: "
printArr(arr, size);
return 0;
}
void printArr(int * arr, int size)
{
int * arrnd = (arr + size - 1);
while(arr <= arrEnd)
{
printf("96d,", art);
ance;
1
}
ourPuT
Enter size of array: 4
Enter elements in array: 82
59
a1
37Array before reverse: 82, 59, 41, 37
Acray after reverse: 37, 41, 59, 82
Pattern Programming
1. Square Star Pattern
include
int main()
{
inti, j,N;
printf("Enter number of rows: ");
scanf("¥éd", &N);
for(i=t; <=; i++)
4
for(j=1; j<=N; j++)
(
printf”
printf("\n");
}
return 0;
}
ourpuT
Enter number of rows: 10
2. Right Triangle Square Pattern
include
int main()
{
inti jn;printf ("Enter value of n:
seant("%al, &n);
forli=t; ic=n; it)
1
for(i=4;je=is +4)
{
printf”
)
printf("\n");
}
return 0;
)
ourpuT
Enter value of n: 10
3. Mirrored Right Triangle Star Pattern
ude
int main()
{
inti, j, rows;
printf("Enter number of row:
scanf("%d", &rows);
"Ii
forli=1; icerows; i++)
{
for(jsi;jerows; j++)
{
printf("");
}
For(j=1;
int main()
{
int rows, cols, i,j;
printf("Enter number of rows: ");
scant("%d", &rows);
printf("Enter number of columns: ");
scanf("9%d", &cols);
forli=1; ie=rows; i++)
{
forl)=1; j<=cols; j++)
{
printf("2");
}
printf("\n"};
}
return 0;
}
ourPuT
Enter number of rows: 4
Enter number of columns: 5
11
11
auaia
111aunt
5. Square Number Pattern 2
include
int main()
{
int rows, cols, i,j;
printf("Enter number of row:
scanf("%d", Brows);
printf("Enter number of columns:
scanf{"%d", &cols};
forli=1; i<=rows; i++)
{
forl)=1; j<=cols; j++)
{
if(62 == 1)
{
printf("2");
}
else
{
printf("0");
}
)
printf("\n");
}
return 0;
}
ouTPUT
Enter number of rows: 5
Enter number of columns: 6
1111
00000
1111
00000
11111