0% found this document useful (0 votes)
27 views44 pages

LIST OF PROGRAM-WPS Office

The document contains a list of 30 programming exercises in C, covering fundamental concepts such as swapping numbers, finding the largest and smallest numbers, temperature conversion, and calculating interest. It also includes tasks for checking even/odd numbers, leap years, prime numbers, and working with arrays (sorting, reversing, and searching). Each exercise is accompanied by code snippets demonstrating the implementation.

Uploaded by

8874989398sv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views44 pages

LIST OF PROGRAM-WPS Office

The document contains a list of 30 programming exercises in C, covering fundamental concepts such as swapping numbers, finding the largest and smallest numbers, temperature conversion, and calculating interest. It also includes tasks for checking even/odd numbers, leap years, prime numbers, and working with arrays (sorting, reversing, and searching). Each exercise is accompanied by code snippets demonstrating the implementation.

Uploaded by

8874989398sv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

LIST OF PROGRAMS

1. Swap two numbers (using a third variable and without a third variable)

 Using third variable

#include <stdio.h>

int main() {

int a, b, temp;

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

temp = a;

a = b;

b = temp;

printf("After swapping: a = %d, b = %d\n", a, b);

return 0;

 Without using third Variable

#include <stdio.h>

int main() {

int a, b;

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

a = a + b;

b = a - b;

a = a - b;

printf("After swapping: a = %d, b = %d\n", a, b);


return 0;

2. Find the largest and smallest of two numbers

#include <stdio.h>

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

if (num1 > num2) {

printf("Largest = %d\nSmallest = %d\n", num1, num2);

} else {

printf("Largest = %d\nSmallest = %d\n", num2, num1);

return 0;

3. Convert temperature from Celsius to Fahrenheit and vice versa

#include <stdio.h>

int main() {

int choice;

float temp, convertedTemp;

printf("Choose conversion:\n1. Celsius to Fahrenheit\n2. Fahrenheit to Celsius\n");

scanf("%d", &choice);

if (choice == 1) {

printf("Enter temperature in Celsius: ");


scanf("%f", &temp);

convertedTemp = (temp * 9 / 5) + 32;

printf("Temperature in Fahrenheit: %.2f\n", convertedTemp);

} else if (choice == 2) {

printf("Enter temperature in Fahrenheit: ");

scanf("%f", &temp);

convertedTemp = (temp - 32) * 5 / 9;

printf("Temperature in Celsius: %.2f\n", convertedTemp);

} else {

printf("Invalid choice!\n");

return 0;

4. Find the ASCII value of a character

#include <stdio.h>

int main() {

char ch;

printf("Enter a character: ");

scanf("%c", &ch);

printf("ASCII value of '%c' = %d\n", ch, ch);

return 0;

5. Calculate the simple interest and compound interest.

#include <stdio.h>
#include <math.h>

int main() {

float principal, rate, time, SI, CI;

printf("Enter principal amount: ");

scanf("%f", &principal);

printf("Enter annual interest rate (in percentage): ");

scanf("%f", &rate);

printf("Enter time in years: ");

scanf("%f", &time);

SI = (principal * rate * time) / 100;

printf("Simple Interest = %.2f\n", SI);

CI = principal * pow((1 + rate / 100), time) - principal;

printf("Compound Interest = %.2f\n", CI);

return 0;

6.Check if a number is even or odd.

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 2 == 0)

printf("Even\n");

else

printf("Odd\n");
return 0;

7. Check if a number is positive, negative, or zero.

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num > 0)

printf("Positive\n");

else if (num < 0)

printf("Negative\n");

else

printf("Zero\n");

return 0;

8. Find the largest of three numbers

#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three numbers: ");

scanf("%d %d %d", &a, &b, &c);

if (a >= b && a >= c)

printf("Largest = %d\n", a);


else if (b >= a && b >= c)

printf("Largest = %d\n", b);

else

printf("Largest = %d\n", c);

return 0;

9. Check if a year is a leap year

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

printf("Leap Year\n");

else

printf("Not a Leap Year\n");

return 0;

10. Check if a number is divisible by 5 and 11

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);
if (num % 5 == 0 && num % 11 == 0)

printf("Divisible by 5 and 11\n");

else

printf("Not divisible by 5 and 11\n");

return 0;

11. Check if a character is a vowel or consonant

#include <stdio.h>

int main() {

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||

ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

printf("Vowel\n");

else

printf("Consonant\n");

return 0;

12. Check if a person is eligible to vote (age > 18)

#include <stdio.h>

int main() {

int age;

printf("Enter age: ");


scanf("%d", &age);

if (age >= 18)

printf("Eligible to vote\n");

else

printf("Not eligible to vote\n");

return 0;

13. Find the grade of a student based on marks

#include <stdio.h>

int main() {

int marks;

printf("Enter marks: ");

scanf("%d", &marks);

if (marks >= 90)

printf("Grade: A\n");

else if (marks >= 80)

printf("Grade: B\n");

else if (marks >= 70)

printf("Grade: C\n");

else if (marks >= 60)

printf("Grade: D\n");

else if (marks >= 50)

printf("Grade: E\n");

else

printf("Grade: F\n");
return 0;

14. Print numbers from 1 to N

#include <stdio.h>

int main() {

int n, i;

printf("Enter N: ");

scanf("%d", &n);

for (i = 1; i <= n; i++)

printf("%d ", i);

printf("\n");

return 0;

15. Print the sum of the first N natural numbers

#include <stdio.h>

int main() {

int n, sum = 0, i;

printf("Enter N: ");

scanf("%d", &n);

for (i = 1; i <= n; i++)

sum += i;

printf("Sum = %d\n", sum);

return 0;

16. Find the sum of even and odd numbers up to N

#include <stdio.h>
int main() {

int n, i, evenSum = 0, oddSum = 0;

printf("Enter N: ");

scanf("%d", &n);

for (i = 1; i <= n; i++) {

if (i % 2 == 0)

evenSum += i;

else

oddSum += i;

printf("Sum of even numbers = %d\n", evenSum);

printf("Sum of odd numbers = %d\n", oddSum);

return 0;

17. Print multiplication table of any number

#include <stdio.h>

int main() {

int n, i;

printf("Enter a number: ");

scanf("%d", &n);

for (i = 1; i <= 10; i++)

printf("%d x %d = %d\n", n, i, n * i);

return 0;

18. Print factorial of a number

#include <stdio.h>
int main() {

int n, i;

long long fact = 1;

printf("Enter a number: ");

scanf("%d", &n);

for (i = 1; i <= n; i++)

fact *= i;

printf("Factorial = %lld\n", fact);

return 0;

19. Print Fibonacci series up to N terms

#include <stdio.h>

int main() {

int n, i, a = 0, b = 1, next;

printf("Enter number of terms: ");

scanf("%d", &n);

for (i = 1; i <= n; i++) {

printf("%d ", a);

next = a + b;

a = b;

b = next;

printf("\n");

return 0;

}
20. Check if a number is prime

#include <stdio.h>

int main() {

int n, i, flag = 1;

printf("Enter a number: ");

scanf("%d", &n);

if (n <= 1)

flag = 0;

for (i = 2; i * i <= n; i++) {

if (n % i == 0) {

flag = 0;

break;

if (flag)

printf("Prime\n");

else

printf("Not Prime\n");

return 0;

21. Find the sum of digits of a number

#include <stdio.h>

int main() {

int n, sum = 0;

printf("Enter a number: ");

scanf("%d", &n);
while (n > 0) {

sum += n % 10;

n /= 10;

printf("Sum of digits = %d\n", sum);

return 0;

22. Reverse a given number

#include <stdio.h>

int main() {

int n, rev = 0;

printf("Enter a number: ");

scanf("%d", &n);

while (n > 0) {

rev = rev * 10 + n % 10;

n /= 10;

printf("Reversed number = %d\n", rev);

return 0;

23. Check if a number is palindrome

#include <stdio.h>

int main() {

int n, temp, rev = 0;

printf("Enter a number: ");

scanf("%d", &n);
temp = n;

while (temp > 0) {

rev = rev * 10 + temp % 10;

temp /= 10;

if (n == rev)

printf("Palindrome\n");

else

printf("Not Palindrome\n");

return 0;

24. Find the GCD and LCM of two numbers

#include <stdio.h>

int main() {

int a, b, x, y, gcd, lcm;

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

x = a;

y = b;

while (y != 0) {

int temp = y;

y = x % y;

x = temp;

gcd = x;

lcm = (a * b) / gcd;
printf("GCD = %d\nLCM = %d\n", gcd, lcm);

return 0;

25. Print all Armstrong numbers between 1 to 1000.

#include <stdio.h>

int main() {

int num, temp, remainder, sum, digits;

for (num = 1; num <= 1000; num++) {

temp = num;

sum = 0;

digits = 0;

while (temp > 0) {

temp /= 10;

digits++;

temp = num;

while (temp > 0) {

remainder = temp % 10;

int power = 1, i;

for (i = 0; i < digits; i++)

power *= remainder;

sum += power;

temp /= 10;

if (sum == num)

printf("%d ", num);


}

printf("\n");

return 0;

26. Find the largest and smallest elements in an array

#include <stdio.h>

int main() {

int n, i, max, min;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);

for (i = 0; i < n; i++)

scanf("%d", &arr[i]);

max = min = arr[0];

for (i = 1; i < n; i++) {

if (arr[i] > max)

max = arr[i];

if (arr[i] < min)

min = arr[i];

printf("Largest = %d\nSmallest = %d\n", max, min);

return 0;

27. Reverse an array


#include <stdio.h>

int main() {

int n, i;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);

for (i = 0; i < n; i++)

scanf("%d", &arr[i]);

printf("Reversed array: ");

for (i = n - 1; i >= 0; i--)

printf("%d ", arr[i]);

printf("\n");

return 0;

28. Sort an array using Bubble Sort

#include <stdio.h>

int main() {

int n, i, j, temp;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);

for (i = 0; i < n; i++)


scanf("%d", &arr[i]);

for (i = 0; i < n - 1; i++) {

for (j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

printf("Sorted array: ");

for (i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

return 0;

29. Find the sum and average of array elements

#include <stdio.h>

int main() {

int n, i, sum = 0;

float avg;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);


for (i = 0; i < n; i++) {

scanf("%d", &arr[i]);

sum += arr[i];

avg = (float)sum / n;

printf("Sum = %d\nAverage = %.2f\n", sum, avg);

return 0;

30. Search an element in an array (Linear Search & Binary Search)

 For Linear Search

#include <stdio.h>

int main() {

int n, i, key, found = 0;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);

for (i = 0; i < n; i++)

scanf("%d", &arr[i]);

printf("Enter the element to search: ");

scanf("%d", &key);

for (i = 0; i < n; i++) {

if (arr[i] == key) {

found = 1;

break;

}
}

if (found)

printf("Element found at index %d\n", i);

else

printf("Element not found\n");

return 0;

 For Binary Search


#include <stdio.h>
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] == key)
return mid;
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}

int main() {
int n, i, key;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d sorted elements: ", n);
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Enter the element to search: ");
scanf("%d", &key);
int result = binarySearch(arr, n, key);
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}

31. Merge two sorted arrays

#include <stdio.h>

int main() {

int n1, n2, i, j, k;

printf("Enter the size of first array: ");

scanf("%d", &n1);

int arr1[n1];

printf("Enter %d sorted elements: ", n1);

for (i = 0; i < n1; i++)

scanf("%d", &arr1[i]);

printf("Enter the size of second array: ");

scanf("%d", &n2);

int arr2[n2], merged[n1 + n2];

printf("Enter %d sorted elements: ", n2);

for (i = 0; i < n2; i++)

scanf("%d", &arr2[i]);

i = j = k = 0;

while (i < n1 && j < n2) {

if (arr1[i] < arr2[j])

merged[k++] = arr1[i++];

else

merged[k++] = arr2[j++];

}
while (i < n1)

merged[k++] = arr1[i++];

while (j < n2)

merged[k++] = arr2[j++];

printf("Merged sorted array: ");

for (i = 0; i < n1 + n2; i++)

printf("%d ", merged[i]);

printf("\n");

return 0;

32. Find the second largest and second smallest elements in an array

#include <stdio.h>

int main() {

int n, i;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements: ", n);

for (i = 0; i < n; i++)

scanf("%d", &arr[i]);

for (i = 0; i < n - 1; i++) {

for (int j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];


arr[j + 1] = temp;

printf("Second Smallest = %d\nSecond Largest = %d\n", arr[1], arr[n - 2]);

return 0;

33. Find the length of a string without using strlen()

#include <stdio.h>

int main() {

char str[100];

int i = 0;

printf("Enter a string: ");

gets(str);

while (str[i] != '\0')

i++;

printf("Length of the string = %d\n", i);

return 0;

34. Reverse a string

#include <stdio.h>

#include <string.h>

int main() {

char str[100], temp;


int i, j;

printf("Enter a string: ");

gets(str);

j = strlen(str) - 1;

for (i = 0; i < j; i++, j--) {

temp = str[i];

str[i] = str[j];

str[j] = temp;

printf("Reversed string: %s\n", str);

return 0;

35. Check if a string is a palindrome

#include <stdio.h>

#include <string.h>

int main() {

char str[100];

int i, j, flag = 1;

printf("Enter a string: ");

gets(str);

j = strlen(str) - 1;

for (i = 0; i < j; i++, j--) {

if (str[i] != str[j]) {

flag = 0;

break;
}

if (flag)

printf("Palindrome\n");

else

printf("Not a palindrome\n");

return 0;

36. Count vowels and consonants in a string

#include <stdio.h>

int main() {

char str[100];

int i, vowels = 0, consonants = 0;

printf("Enter a string: ");

gets(str);

for (i = 0; str[i] != '\0'; i++) {

char ch = str[i];

if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||

ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

vowels++;

else

consonants++;

printf("Vowels = %d\nConsonants = %d\n", vowels, consonants);


return 0;

37. Convert uppercase to lowercase and vice versa

#include <stdio.h>

int main() {

char str[100];

int i;

printf("Enter a string: ");

gets(str);

for (i = 0; str[i] != '\0'; i++) {

if (str[i] >= 'a' && str[i] <= 'z')

str[i] -= 32;

else if (str[i] >= 'A' && str[i] <= 'Z')

str[i] += 32;

printf("Converted string: %s\n", str);

return 0;

38. Find the frequency of each character in a string

#include <stdio.h>

#include <string.h>

int main() {

char str[100];

int freq[256] = {0}, i;

printf("Enter a string: ");

gets(str);
for (i = 0; str[i] != '\0'; i++)

freq[(int)str[i]]++;

printf("Character frequencies:\n");

for (i = 0; i < 256; i++) {

if (freq[i] > 0)

printf("%c = %d\n", i, freq[i]);

return 0;

39. Find the number of words in a string

#include <stdio.h>

int main() {

char str[100];

int i, count = 1;

printf("Enter a string: ");

gets(str);

for (i = 0; str[i] != '\0'; i++) {

if (str[i] == ' ' && str[i + 1] != ' ' && str[i + 1] != '\0')

count++;

printf("Number of words = %d\n", count);

return 0;

40. Write a function to calculate the factorial of a number

#include <stdio.h>

int factorial(int n) {
int fact = 1;

for (int i = 1; i <= n; i++)

fact *= i;

return fact;

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("Factorial = %d\n", factorial(num));

return 0;

41. Write a function to check if a number is prime

#include <stdio.h>

int isPrime(int n) {

if (n < 2)

return 0;

for (int i = 2; i * i <= n; i++) {

if (n % i == 0)

return 0;

return 1;

int main() {
int num;

printf("Enter a number: ");

scanf("%d", &num);

if (isPrime(num))

printf("Prime\n");

else

printf("Not Prime\n");

return 0;

42. Write a function to swap two numbers

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x, y;

printf("Enter two numbers: ");

scanf("%d %d", &x, &y);

swap(&x, &y);

printf("After swapping: %d %d\n", x, y);

return 0;

43. Write a recursive function to find the Fibonacci series


#include <stdio.h>

int fibonacci(int n) {

if (n <= 1)

return n;

return fibonacci(n - 1) + fibonacci(n - 2);

int main() {

int n;

printf("Enter the number of terms: ");

scanf("%d", &n);

for (int i = 0; i < n; i++)

printf("%d ", fibonacci(i));

return 0;

44. Find the power of a number using a function

#include <stdio.h>

int power(int base, int exp) {

int result = 1;

for (int i = 0; i < exp; i++)

result *= base;

return result;

int main() {

int base, exp;

printf("Enter base and exponent: ");


scanf("%d %d", &base, &exp);

printf("Result = %d\n", power(base, exp));

return 0;

45. Write a function to find the sum of digits of a number

#include <stdio.h>

int sumOfDigits(int n) {

int sum = 0;

while (n > 0) {

sum += n % 10;

n /= 10;

return sum;

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("Sum of digits = %d\n", sumOfDigits(num));

return 0;

46. Write a function to convert decimal to binary

#include <stdio.h>

void decimalToBinary(int n) {

if (n > 0) {
decimalToBinary(n / 2);

printf("%d", n % 2);

int main() {

int num;

printf("Enter a decimal number: ");

scanf("%d", &num);

if (num == 0)

printf("0");

else

decimalToBinary(num);

return 0;

47. Swap two numbers using pointers

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

int main() {

int x, y;

printf("Enter two numbers: ");

scanf("%d %d", &x, &y);

swap(&x, &y);
printf("After swapping: %d %d\n", x, y);

return 0;

48. Find the sum of array elements using pointers

#include <stdio.h>

int sum(int *arr, int n) {

int total = 0;

for (int i = 0; i < n; i++)

total += *(arr + i);

return total;

int main() {

int arr[5], n = 5;

printf("Enter 5 numbers: ");

for (int i = 0; i < n; i++)

scanf("%d", &arr[i]);

printf("Sum = %d\n", sum(arr, n));

return 0;

49. Reverse a string using pointers

#include <stdio.h>

void reverse(char *str) {

char *start = str;

char *end = str;

char temp;

while (*end != '\0')


end++;

end--;

while (start < end) {

temp = *start;

*start = *end;

*end = temp;

start++;

end--;

int main() {

char str[100];

printf("Enter a string: ");

gets(str);

reverse(str);

printf("Reversed string: %s\n", str);

return 0;

50. Find the largest and smallest elements in an array using pointers

#include <stdio.h>

void findMinMax(int *arr, int n, int *min, int *max) {

*min = *max = *arr;

for (int i = 1; i < n; i++) {

if (*(arr + i) < *min)

*min = *(arr + i);


if (*(arr + i) > *max)

*max = *(arr + i);

int main() {

int arr[5], min, max, n = 5;

printf("Enter 5 numbers: ");

for (int i = 0; i < n; i++)

scanf("%d", &arr[i]);

findMinMax(arr, n, &min, &max);

printf("Min = %d, Max = %d\n", min, max);

return 0;

51. Copy one string to another using pointers

#include <stdio.h>

void copyString(char *src, char *dest) {

while (*src) {

*dest = *src;

src++;

dest++;

*dest = '\0';

int main() {

char src[100], dest[100];


printf("Enter a string: ");

gets(src);

copyString(src, dest);

printf("Copied string: %s\n", dest);

return 0;

52. Add two matrices

#include <stdio.h>

int main() {

int A[3][3], B[3][3], sum[3][3];

printf("Enter elements of matrix A (3x3):\n");

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

scanf("%d", &A[i][j]);

printf("Enter elements of matrix B (3x3):\n");

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

scanf("%d", &B[i][j]);

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

sum[i][j] = A[i][j] + B[i][j];

printf("Sum of matrices:\n");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++)

printf("%d ", sum[i][j]);

printf("\n");
}

return 0;

53. Subtract two matrices

#include <stdio.h>

int main() {

int A[3][3], B[3][3], diff[3][3];

printf("Enter elements of matrix A (3x3):\n");

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

scanf("%d", &A[i][j]);

printf("Enter elements of matrix B (3x3):\n");

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

scanf("%d", &B[i][j]);

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

diff[i][j] = A[i][j] - B[i][j];

printf("Difference of matrices:\n");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++)

printf("%d ", diff[i][j]);

printf("\n");

return 0;

}
54. Multiply two matrices

#include <stdio.h>

int main() {

int A[3][3], B[3][3], mul[3][3] = {0};

printf("Enter elements of matrix A (3x3):\n");

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

scanf("%d", &A[i][j]);

printf("Enter elements of matrix B (3x3):\n");

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

scanf("%d", &B[i][j]);

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

for (int k = 0; k < 3; k++)

mul[i][j] += A[i][k] * B[k][j];

printf("Product of matrices:\n");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++)

printf("%d ", mul[i][j]);

printf("\n");

return 0;

55. Find the transpose of a matrix

#include <stdio.h>
int main() {

int A[3][3], trans[3][3];

printf("Enter elements of matrix A (3x3):\n");

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

scanf("%d", &A[i][j]);

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

trans[i][j] = A[j][i];

printf("Transpose of matrix A:\n");

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++)

printf("%d ", trans[i][j]);

printf("\n");

return 0;

56. Check if a matrix is symmetric

#include <stdio.h>

int main() {

int A[3][3], i, j, flag = 1;

printf("Enter elements of matrix A (3x3):\n");

for (i = 0; i < 3; i++)

for (j = 0; j < 3; j++)

scanf("%d", &A[i][j]);

for (i = 0; i < 3; i++) {


for (j = 0; j < 3; j++) {

if (A[i][j] != A[j][i]) {

flag = 0;

break;

if (flag)

printf("Matrix is symmetric\n");

else

printf("Matrix is not symmetric\n");

return 0;

57. Define a structure for student details (name, roll number, marks) and print the details

#include <stdio.h>

struct Student {

char name[100];

int roll_no;

float marks;

};

int main() {

struct Student student1;

printf("Enter student name: ");

gets(student1.name);

printf("Enter roll number: ");


scanf("%d", &student1.roll_no);

printf("Enter marks: ");

scanf("%f", &student1.marks);

printf("\nStudent Details:\n");

printf("Name: %s\n", student1.name);

printf("Roll Number: %d\n", student1.roll_no);

printf("Marks: %.2f\n", student1.marks);

return 0;

58. Define a structure for employee details and display the highest salary

#include <stdio.h>

struct Employee {

char name[100];

int emp_id;

float salary;

};

int main() {

struct Employee emp1, emp2, emp3;

printf("Enter details of Employee 1:\n");

printf("Enter name: ");

gets(emp1.name);

printf("Enter employee ID: ");

scanf("%d", &emp1.emp_id);

printf("Enter salary: ");


scanf("%f", &emp1.salary);

printf("\nEnter details of Employee 2:\n");

printf("Enter name: ");

gets(emp2.name);

printf("Enter employee ID: ");

scanf("%d", &emp2.emp_id);

printf("Enter salary: ");

scanf("%f", &emp2.salary);

printf("\nEnter details of Employee 3:\n");

printf("Enter name: ");

gets(emp3.name);

printf("Enter employee ID: ");

scanf("%d", &emp3.emp_id);

printf("Enter salary: ");

scanf("%f", &emp3.salary);

if (emp1.salary > emp2.salary && emp1.salary > emp3.salary)

printf("Employee with highest salary: %s\n", emp1.name);

else if (emp2.salary > emp1.salary && emp2.salary > emp3.salary)

printf("Employee with highest salary: %s\n", emp2.name);

else

printf("Employee with highest salary: %s\n", emp3.name);

return 0;
}

59. Define a structure for a book (title, author, price) and sort books by price

#include <stdio.h>

#include <string.h>

struct Book {

char title[100];

char author[100];

float price;

};

int main() {

struct Book book1, book2, book3;

printf("Enter details of Book 1:\n");

printf("Enter title: ");

gets(book1.title);

printf("Enter author: ");

gets(book1.author);

printf("Enter price: ");

scanf("%f", &book1.price);

printf("\nEnter details of Book 2:\n");

printf("Enter title: ");

gets(book2.title);

printf("Enter author: ");

gets(book2.author);
printf("Enter price: ");

scanf("%f", &book2.price);

printf("\nEnter details of Book 3:\n");

printf("Enter title: ");

gets(book3.title);

printf("Enter author: ");

gets(book3.author);

printf("Enter price: ");

scanf("%f", &book3.price);

if (book1.price > book2.price && book1.price > book3.price) {

printf("\nMost expensive book: %s\n", book1.title);

} else if (book2.price > book1.price && book2.price > book3.price) {

printf("\nMost expensive book: %s\n", book2.title);

} else {

printf("\nMost expensive book: %s\n", book3.title);

return 0;

---------------------------------------------------------------------
END-----------------------------------------------------------------------------

You might also like