├── .gitignore ├── README.md └── programs ├── others ├── helloworld.c ├── swap.c ├── string.c ├── reverse.c ├── struct_union_diff.c ├── testing.c ├── palindrome_number.c ├── file_exam_ques.c ├── real_img.c ├── armstrong.c ├── BubbleSort.c ├── employee_sort.c └── array.c ├── tutorial2 ├── 1pint_name.c ├── 5ascii.c ├── 3avg.c ├── 4multiply.c ├── 18sqrt.c ├── 9oddeven.c ├── 2printnums.c ├── 6swap.c ├── 20maxmin.c ├── 14alpha.c ├── 17op.c ├── 10leap.c ├── 15sign.c ├── 8largest.c ├── 7vovel.c ├── 13shiftop.c ├── 16grade.c ├── 12calculator.c ├── tempCodeRunnerFile.c ├── 19interest.c └── 11roots.c ├── record ├── exp16.c ├── exp8.c ├── exp19.c ├── exp7.c ├── exp9.c ├── exp13.c ├── exp15.c ├── exp10.c ├── exp12.c ├── exp17.c ├── exp20.c ├── exp14.c ├── exp18.c └── exp11.c ├── exam ├── leap.c ├── string_reverse.c ├── large_small.c └── prime_arm_pref_odd_even.c ├── patterns ├── p1.c └── p2.c ├── tutorial-3-8-22 ├── sizeOfUnionAndStruct.c ├── q4.c ├── sumOfDiagonals.c ├── q2.c └── q3.c ├── sorting ├── insertionSort.c └── selectionSort.c ├── struct ├── struct1.c └── struct2.c └── stack └── stack.c /.gitignore: -------------------------------------------------------------------------------- 1 | # "hack" to exclude compiled files 2 | * 3 | !*.* 4 | !*/ 5 | .vscode -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## C Programs 2 | Programs in C for college purposes. 3 | 4 | [xditya.me](https://xditya.me) -------------------------------------------------------------------------------- /programs/others/helloworld.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | printf("Hello, World!\n") -------------------------------------------------------------------------------- /programs/tutorial2/1pint_name.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | printf("My name is Aditya.\n"); 5 | } -------------------------------------------------------------------------------- /programs/tutorial2/5ascii.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | printf("Enter a character: "); 5 | char ch; 6 | scanf("%c", &ch); 7 | printf("The ASCII value of %c is %d.", ch, ch); 8 | } -------------------------------------------------------------------------------- /programs/tutorial2/3avg.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | printf("Enter three numbers:\n"); 5 | int a, b, c; 6 | scanf("%d\n%d\n%d", &a, &b, &c); 7 | printf("The average is %d", (a + b + c) / 3); 8 | } -------------------------------------------------------------------------------- /programs/tutorial2/4multiply.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | printf("Enter two numbers (floating-point): \n"); 5 | float a, b; 6 | scanf("%f\n%f", &a, &b); 7 | printf("%f X %f = %f", a, b, a * b); 8 | } 9 | -------------------------------------------------------------------------------- /programs/tutorial2/18sqrt.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | int n, sq; 6 | printf("Enter a number: "); 7 | scanf("%d", &n); 8 | sq = sqrt(n); 9 | printf("Square root of %d = %d", n, sq); 10 | } -------------------------------------------------------------------------------- /programs/tutorial2/9oddeven.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | printf("Enter a number: "); 5 | int n; 6 | scanf("%d", &n); 7 | if (n % 2 == 0) 8 | printf("%d is even.\n", n); 9 | else 10 | printf("%d is odd", n); 11 | } -------------------------------------------------------------------------------- /programs/tutorial2/2printnums.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | printf("Integer number: %d, Size: %d\n", 5, sizeof(5)); 5 | printf("Floating-point number: %f, Size: %d\n", 5.0, sizeof(5.0)); 6 | printf("Character: %c, Size: %d\n", 'a', sizeof('a')); 7 | } -------------------------------------------------------------------------------- /programs/others/swap.c: -------------------------------------------------------------------------------- 1 | // swap without a third var 2 | 3 | #include 4 | void main() 5 | { 6 | int a = 5, b = 6; 7 | printf("\na = %d\nb = %d", a, b); 8 | a = a + b; 9 | b = a - b; 10 | a = a - b; 11 | printf("\na = %d\nb = %d", a, b); 12 | } -------------------------------------------------------------------------------- /programs/tutorial2/6swap.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int a = 5, b = 6; 5 | printf("Before swapping.\na = %d\nb = %d\n\n", a, b); 6 | a = a + b; 7 | b = a - b; 8 | a = a - b; 9 | printf("After swapping.\na = %d\nb = %d\n\n", a, b); 10 | } -------------------------------------------------------------------------------- /programs/tutorial2/20maxmin.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int a, b; 5 | printf("Enter two numbers:\n"); 6 | scanf("%d\n%d", &a, &b); 7 | int max = a > b ? a : b; 8 | int min = a < b ? a : b; 9 | printf("Maximum: %d\nMinimum: %d\n", max, min); 10 | } -------------------------------------------------------------------------------- /programs/others/string.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | char str[100]; 7 | printf("Enter a string: "); 8 | gets(str); 9 | if (strcmp(str, strrev(str) == 0)) 10 | printf("Palindrome"); 11 | else 12 | printf("Not palindrome"); 13 | } -------------------------------------------------------------------------------- /programs/others/reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int n, i, rev = 0; 6 | printf("Enter a number: "); 7 | scanf("%d", &n); 8 | while (n > 0) 9 | { 10 | rev = rev * 10 + (n % 10); 11 | n /= 10; 12 | } 13 | printf("The reverse is %d\n", rev); 14 | } -------------------------------------------------------------------------------- /programs/record/exp16.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int sum(int n) 4 | { 5 | if (n == 0) 6 | return 0; 7 | return (n % 10 + sum(n / 10)); 8 | } 9 | 10 | void main() 11 | { 12 | int n; 13 | printf("Enter a number: "); 14 | scanf("%d", &n); 15 | printf("Sum of digits: %d", sum(n)); 16 | } -------------------------------------------------------------------------------- /programs/exam/leap.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int yr; 6 | printf("Enter a year: "); 7 | scanf("%d", &yr); 8 | if (yr % 4 == 0 && (yr % 100 != 0 || yr % 400 == 0)) 9 | printf("%d is a leap year.\n", yr); 10 | else 11 | printf("%d is not a leap year.\n", yr); 12 | } -------------------------------------------------------------------------------- /programs/patterns/p1.c: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | 1 2 4 | 1 2 3 5 | 1 2 3 4 6 | 1 2 3 4 5 7 | */ 8 | 9 | #include 10 | 11 | void main() 12 | { 13 | int i, j; 14 | for (i = 0; i < 5; i++) 15 | { 16 | for (j = 0; j <= i; j++) 17 | printf("%d ", j + 1); 18 | printf("\n"); 19 | } 20 | } -------------------------------------------------------------------------------- /programs/patterns/p2.c: -------------------------------------------------------------------------------- 1 | /* 2 | 1 2 3 4 5 3 | 1 2 3 4 4 | 1 2 3 5 | 1 2 6 | 1 7 | */ 8 | 9 | #include 10 | 11 | void main() 12 | { 13 | int i, j; 14 | for (i = 5; i > 0; i--) 15 | { 16 | for (j = 0; j < i; j++) 17 | printf("%d ", j + 1); 18 | printf("\n"); 19 | } 20 | } -------------------------------------------------------------------------------- /programs/exam/string_reverse.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | char str[100]; 6 | printf("Enter a string: "); 7 | gets(str); 8 | int i, k = 0; 9 | char rev[100]; 10 | for (i = strlen(str) - 1; i >= 0; i--) 11 | rev[k++] = str[i]; 12 | printf("Reversed string = %s\n", rev); 13 | } -------------------------------------------------------------------------------- /programs/tutorial-3-8-22/sizeOfUnionAndStruct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct st 4 | { 5 | int x; 6 | }; 7 | 8 | union sample 9 | { 10 | int m; 11 | }; 12 | 13 | void main() 14 | { 15 | printf("The size of struct = %d\n", sizeof(struct st)); 16 | printf("The size of union = %d\n", sizeof(union sample)); 17 | } -------------------------------------------------------------------------------- /programs/tutorial2/14alpha.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | char ch; 6 | printf("Enter an alphabet: "); 7 | scanf("%c", &ch); 8 | int r = isalpha(ch); 9 | if (r == 0) 10 | printf("%c is not an alphabet.", ch); 11 | else 12 | printf("%c is an alphabet.", ch); 13 | } -------------------------------------------------------------------------------- /programs/tutorial2/17op.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int a, b; 5 | printf("Enter two numbers:\n"); 6 | scanf("%d\n%d", &a, &b); 7 | // 11 & 8, 10 | 2, 5^6 8 | printf("%d & %d = %d\n", a, b, a & b); 9 | printf("%d | %d = %d\n", a, b, a | b); 10 | printf("%d ^ %d = %d\n", a, b, a ^ b); 11 | } -------------------------------------------------------------------------------- /programs/tutorial2/10leap.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int yr; 5 | printf("Enter year: "); 6 | scanf("%d", &yr); 7 | if ((yr % 4 == 0) && ((yr % 100 != 0 || yr % 400 == 0))) 8 | printf("%d is a leap year", yr); 9 | else 10 | printf("%d is not a leap year", yr); 11 | printf("\n"); 12 | } 13 | -------------------------------------------------------------------------------- /programs/tutorial2/15sign.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | printf("Enter a number: "); 5 | int a; 6 | scanf("%d", &a); 7 | if (a > 0) 8 | printf("%d is positive."); 9 | else if (a < 0) 10 | printf("%d is negative."); 11 | else 12 | printf("%d is neither positive nor negative.", a); 13 | } -------------------------------------------------------------------------------- /programs/tutorial2/8largest.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int a, b, c; 5 | printf("Enter three numbers:\n"); 6 | scanf("%d\n%d\n%d", &a, &b, &c); 7 | int l = a > b && a > c ? a : b > c && b > a ? b 8 | : c; 9 | printf("The largest among %d, %d and %d is %d", a, b, c, l); 10 | } -------------------------------------------------------------------------------- /programs/others/struct_union_diff.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Structure 4 | { 5 | int a; 6 | char b; 7 | }; 8 | 9 | union Union 10 | { 11 | int a; 12 | char ch; 13 | }; 14 | 15 | void main() 16 | { 17 | union Union un; 18 | struct Structure st; 19 | printf("SizeOf Union = %d\nSizeOf Struct = %d\n", sizeof(un), sizeof(st)); 20 | } -------------------------------------------------------------------------------- /programs/record/exp8.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int n, i, sum = 0; 5 | printf("Enter the limit: "); 6 | scanf("%d", &n); 7 | int a[n]; 8 | printf("Enter %d elements: \n", n); 9 | for (i = 0; i < n; i++) 10 | scanf("%d", &a[i]); 11 | for (i = 0; i < n; i++) 12 | sum += a[i]; 13 | printf("The sum is %d\nThe average is %d\n\n", sum, sum / n); 14 | } -------------------------------------------------------------------------------- /programs/others/testing.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | double base, power, result; 7 | 8 | printf("Enter the base number: "); 9 | scanf("%lf", &base); 10 | 11 | printf("Enter the power raised: "); 12 | scanf("%lf", &power); 13 | 14 | result = pow(base, power); 15 | 16 | printf("%.1lf^%.1lf = %.2lf", base, power, result); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /programs/record/exp19.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | int n, i, s = 0; 7 | printf("Enter the number of elements: "); 8 | scanf("%d", &n); 9 | int *p = malloc(n * sizeof(int)); 10 | printf("Enter %d numbers:\n", n); 11 | for (i = 0; i < n; i++) 12 | scanf("%d", p + i); 13 | for (i = 0; i < n; i++) 14 | s += *(p + i); 15 | printf("Sum = %d", s); 16 | } -------------------------------------------------------------------------------- /programs/exam/large_small.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int i, a, b, c, large, small; 6 | printf("Enter 3 numbers:\n"); 7 | scanf("%d\n%d\n%d", &a, &b, &c); 8 | large = a > b && a > c ? a : b > c ? b 9 | : c; 10 | small = a < b && a < c ? a : b < c ? b 11 | : c; 12 | printf("\nLargest = %d\nSmallest = %d\n", large, small); 13 | } -------------------------------------------------------------------------------- /programs/others/palindrome_number.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int n, i, rev = 0, t; 5 | printf("Enter a number: "); 6 | scanf("%d", &n); 7 | t = n; // temporary store 8 | while (n > 0) 9 | { 10 | rev = rev * 10 + (n % 10); 11 | n /= 10; 12 | } 13 | if (t == rev) 14 | printf("%d is a palindrome.\n", t); 15 | else 16 | printf("%d is not a palindrome.\n", t); 17 | } -------------------------------------------------------------------------------- /programs/tutorial2/7vovel.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | printf("Enter a character: "); 6 | char ch; 7 | scanf("%c", &ch); 8 | switch (tolower(ch)) 9 | { 10 | case 'a': 11 | case 'e': 12 | case 'i': 13 | case 'o': 14 | case 'u': 15 | printf("The character %c is a vovel.", ch); 16 | break; 17 | default: 18 | printf("The character is a consonant."); 19 | } 20 | } -------------------------------------------------------------------------------- /programs/tutorial-3-8-22/q4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Complex 4 | { 5 | int real; 6 | int img; 7 | }; 8 | 9 | void main() 10 | { 11 | struct Complex a, b, c; 12 | printf("Enter the real and img part of a:"); 13 | scanf("%d%d", &a.real, &a.img); 14 | printf("Enter the real and img part of b:"); 15 | scanf("%d%d", &b.real, &b.img); 16 | c.real = a.real + b.real; 17 | c.img = a.img + b.img; 18 | printf("c = %d + %di\n", c.real, c.img); 19 | } -------------------------------------------------------------------------------- /programs/others/file_exam_ques.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int n; 6 | FILE *fp; 7 | fp = fopen("Input.txt", "r"); 8 | if (fp == NULL) 9 | { 10 | printf("File Input.txt does not exist!"); 11 | return; 12 | } 13 | fscanf(fp, "%d", &n); 14 | fclose(fp); 15 | fp = fopen("Output.txt", "w"); 16 | if (n % 2 == 0) 17 | fprintf(fp, "%s", "Even"); 18 | else 19 | fprintf(fp, "%s", "Odd"); 20 | fclose(fp); 21 | } -------------------------------------------------------------------------------- /programs/others/real_img.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Numbers 4 | { 5 | int real, img; 6 | }; 7 | 8 | void main() 9 | { 10 | struct Numbers num1, num2, sum; 11 | printf("Enter real and imaginary part of the first number: \n"); 12 | scanf("%d\n%d", &num1.real, &num1.img); 13 | printf("Enter real and imaginary part of the second number: \n"); 14 | scanf("%d\n%d", &num2.real, &num2.img); 15 | sum.real = num1.real + num2.real; 16 | sum.img = num1.img + num2.img; 17 | printf("\nSum = %d + i%d\n", sum.real, sum.img); 18 | } -------------------------------------------------------------------------------- /programs/record/exp7.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int num, large = 0, slarge = 0, i = 0, n; 5 | printf("Enter the limit: "); 6 | scanf("%d", &n); 7 | for (i = 0; i < n; i++) 8 | { 9 | scanf("%d", &num); 10 | if (i == 0) 11 | large = num; 12 | else if (num > large) 13 | { 14 | slarge = large; 15 | large = num; 16 | } 17 | else if (num > slarge) 18 | slarge = num; 19 | } 20 | printf("\nSecond largest = %d", slarge); 21 | } 22 | -------------------------------------------------------------------------------- /programs/record/exp9.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int i, n, s; 5 | printf("Enter limit: "); 6 | scanf("%d", &n); 7 | int a[n]; 8 | printf("Enter %d elements:\n", n); 9 | for (i = 0; i < n; i++) 10 | scanf("%d", &a[i]); 11 | printf("Enter an element to search: "); 12 | scanf("%d", &s); 13 | for (i = 0; i < n; i++) 14 | if (a[i] == s) 15 | { 16 | printf("Element %d found at position %d", s, i + 1); 17 | return; 18 | } 19 | printf("Element not found in array!"); 20 | } -------------------------------------------------------------------------------- /programs/record/exp13.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | char str[20]; 6 | int i, l, tmp = 0; 7 | printf("Enter a string:"); 8 | scanf("%s", str); 9 | l = strlen(str); 10 | for (i = 0; i < l; i++) 11 | { 12 | if (str[i] != str[l - i - 1]) 13 | { 14 | tmp = 1; 15 | break; 16 | } 17 | } 18 | 19 | if (tmp == 0) 20 | { 21 | printf("String is a palindrome"); 22 | } 23 | else 24 | { 25 | printf("String is not a palindrome"); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /programs/tutorial2/13shiftop.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int n, s, ch, res; 5 | printf("Enter the number: "); 6 | scanf("%d", &n); 7 | printf("Enter number of positions to shift: "); 8 | scanf("%d", &s); 9 | printf("Options: \n1. >>\n2. <<\n\nEnter choice: "); 10 | scanf("%d", &ch); 11 | switch (ch) 12 | { 13 | case 1: 14 | res = n >> s; 15 | break; 16 | case 2: 17 | res = n << s; 18 | break; 19 | default: 20 | printf("Wrong choice!"); 21 | return; 22 | } 23 | printf("Result = %d", res); 24 | } -------------------------------------------------------------------------------- /programs/record/exp15.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int isPrime(int n) 4 | { 5 | int i, c = 0; 6 | for (i = 1; i <= n; i++) 7 | if (n % i == 0) 8 | c++; 9 | if (c == 2) 10 | return 1; 11 | return 0; 12 | } 13 | 14 | void main() 15 | { 16 | int n, i = 0, c = 0; 17 | printf("Enter the limit: "); 18 | scanf("%d", &n); 19 | printf("\n\nThe first %d prime numbers are: \n", n); 20 | while (c < n) 21 | { 22 | if (isPrime(i) == 1) 23 | { 24 | c++; 25 | printf("%d\n", i); 26 | } 27 | i++; 28 | } 29 | } -------------------------------------------------------------------------------- /programs/record/exp10.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int i, j, n, t; 5 | printf("Enter the number of numbers: "); 6 | scanf("%d", &n); 7 | int a[n]; 8 | printf("Enter %d numbers:\n", n); 9 | for (i = 0; i < n; i++) 10 | scanf("%d", &a[i]); 11 | 12 | for (i = 0; i < n - 1; i++) 13 | for (j = 0; j < n - 1 - i; j++) 14 | if (a[j] > a[j + 1]) 15 | { 16 | t = a[j]; 17 | a[j] = a[j + 1]; 18 | a[j + 1] = t; 19 | } 20 | 21 | printf("\nSorted numbers: \n"); 22 | for (i = 0; i < n; i++) 23 | printf("%d ", a[i]); 24 | } -------------------------------------------------------------------------------- /programs/others/armstrong.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | int n, s = 0, l = 0, temp, i, d; 7 | printf("Enter a number: "); 8 | scanf("%d", &n); 9 | temp = n; 10 | // find length of number 11 | while (temp > 0) 12 | { 13 | l++; 14 | temp /= 10; 15 | } 16 | temp = n; 17 | // power and other stuff 18 | for (i = n; i > 0; i /= 10) 19 | { 20 | d = n % 10; 21 | s += pow(d, l); 22 | n /= 10; 23 | } 24 | if (s == temp) 25 | printf("%d is an armstrong number.", temp); 26 | else 27 | printf("%d is not an armstrong number.", temp); 28 | } -------------------------------------------------------------------------------- /programs/tutorial2/16grade.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int marks; 5 | printf("Enter marks: "); 6 | scanf("%d", &marks); 7 | char grade; 8 | if (marks > 100 || marks < 0) 9 | { 10 | printf("Invalid mark entered!\n"); 11 | return; 12 | } 13 | int m = (marks / 10); 14 | if (m == 10 || m == 9) 15 | grade = 'S'; 16 | else if (m == 8) 17 | grade = 'A'; 18 | else if (m == 7) 19 | grade = 'B'; 20 | else if (m == 6) 21 | grade = 'C'; 22 | else if (m == 5) 23 | grade = 'D'; 24 | else 25 | grade = 'F'; 26 | printf("Your marks: %d\nGrade: %c\n", marks, grade); 27 | } 28 | -------------------------------------------------------------------------------- /programs/record/exp12.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { 5 | int m, n, i, j, s = 0; 6 | printf("Enter the number of rows and coloumns: "); 7 | scanf("%d\n%d", &m, &n); 8 | int a[m][n]; 9 | printf("Enter %d numbers:\n", m * n); 10 | for (i = 0; i < m; i++) 11 | for (j = 0; j < n; j++) 12 | { 13 | scanf("%d", &a[i][j]); 14 | if (i == j) 15 | s += a[i][j]; 16 | } 17 | printf("The matrix:\n"); 18 | for (i = 0; i < m; i++) 19 | { 20 | for (j = 0; j < n; j++) 21 | printf("%d ", a[i][j]); 22 | printf("\n"); 23 | } 24 | printf("\nThe sum of the diagonals is %d", s); 25 | } -------------------------------------------------------------------------------- /programs/sorting/insertionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int arr[100], n, i, j, temp; 4 | printf("Please Enter the total Number of Elements : "); 5 | scanf("%d", &n); 6 | printf("Please Enter the Array Elements : "); 7 | for(i = 0; i < n; i++) 8 | scanf("%d", &arr[i]); 9 | for(i = 1; i <= n - 1; i++) { 10 | for(j = i; j > 0 && arr[j - 1] > arr[j]; j--) 11 | { 12 | temp = arr[j]; 13 | arr[j] = arr[j - 1]; 14 | arr[j - 1] = temp; 15 | } 16 | } 17 | printf("Insertion Sort Result : "); 18 | for(i = 0; i < n; i++) { 19 | printf("%d ", arr[i]); 20 | } 21 | printf("\n"); 22 | } -------------------------------------------------------------------------------- /programs/sorting/selectionSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int arr[100], n, i, j, temp; 4 | printf("Please Enter the total Number of Elements: "); 5 | scanf("%d", &n); 6 | printf("Please Enter the Array Elements: "); 7 | for(i=0; i arr[j]) { 12 | temp = arr[i]; 13 | arr[i] = arr[j]; 14 | arr[j] = temp; 15 | } 16 | } 17 | } 18 | printf("\nSelection Sort Result: "); 19 | for(i = 0; i < n; i++) { 20 | printf("%d ", arr[i]); 21 | } 22 | printf("\n"); 23 | } -------------------------------------------------------------------------------- /programs/tutorial-3-8-22/sumOfDiagonals.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void sumOfDiagonals(int m, int n) 4 | { 5 | int i, j, s = 0, a[m][n]; 6 | printf("Enter %d numbers: ", m * n); 7 | for (i = 0; i < m; i++) 8 | for (j = 0; j < n; j++) 9 | { 10 | scanf("%d", &a[i][j]); 11 | if (i == j) 12 | s += a[i][j]; 13 | } 14 | printf("The sum of diagonals is: %d", s); 15 | } 16 | 17 | void main() 18 | { 19 | printf("Enter the number of rows and coloumns: "); 20 | int m, n; 21 | scanf("%d\n%d", &m, &n); 22 | if (m != n) 23 | { 24 | printf("Matrix does not have a diagonal!"); 25 | return; 26 | } 27 | else 28 | sumOfDiagonals(m, n); 29 | } -------------------------------------------------------------------------------- /programs/record/exp17.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct Employee 5 | { 6 | int id; 7 | float salary; 8 | char name[30]; 9 | }; 10 | 11 | void main() 12 | { 13 | int n, i; 14 | printf("Enter number of employees: "); 15 | scanf("%d", &n); 16 | struct Employee e; 17 | for (i = 1; i <= n; i++) 18 | { 19 | printf("\nEmployee %d\n", i); 20 | printf("Enter ID: "); 21 | scanf("%d", &e.id); 22 | printf("Enter name: "); 23 | gets(e.name); 24 | printf("Enter salary: "); 25 | scanf("%f", &e.salary); 26 | printf("\nEntered detail is:"); 27 | printf("Name: %s", e.name); 28 | printf("Id: %d", e.id); 29 | printf("Salary: %f\n", e.salary); 30 | } 31 | } -------------------------------------------------------------------------------- /programs/tutorial2/12calculator.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | printf("Available operations: \n1. *\n2. /\n3. +\n4. -\n5. mod\n\nEnter your choice number: "); 5 | int ch; 6 | scanf("%d", &ch); 7 | printf("Enter two numbers: \n"); 8 | int a, b, res; 9 | scanf("%d\n%d", &a, &b); 10 | switch (ch) 11 | { 12 | case 1: 13 | res = a * b; 14 | break; 15 | case 2: 16 | res = a / b; 17 | break; 18 | case 3: 19 | res = a + b; 20 | break; 21 | case 4: 22 | res = a - b; 23 | break; 24 | case 5: 25 | res = a % b; 26 | break; 27 | default: 28 | printf("Wrong choice!"); 29 | return; 30 | } 31 | printf("Result = %d", res); 32 | } -------------------------------------------------------------------------------- /programs/tutorial2/tempCodeRunnerFile.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | printf("Available operations: \n1. *\n2. /\n3. +\n4. -\n5. mod\n\nEnter your choice number: "); 5 | int ch; 6 | scanf("%d", &ch); 7 | printf("Enter two numbers: \n"); 8 | int a, b, res; 9 | scanf("%d\n%d", &a, &b); 10 | switch (ch) 11 | { 12 | case 1: 13 | res = a * b; 14 | break; 15 | case 2: 16 | res = a / b; 17 | break; 18 | case 3: 19 | res = a + b; 20 | break; 21 | case 4: 22 | res = a - b; 23 | break; 24 | case 5: 25 | res = a % b; 26 | break; 27 | default: 28 | printf("Wrong choice!"); 29 | return; 30 | } 31 | printf("Result = %d", res); 32 | } -------------------------------------------------------------------------------- /programs/others/BubbleSort.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main() 3 | { 4 | int i, j, temp, n; 5 | printf("Enter the limit: "); 6 | scanf("%d", &n); 7 | int a[n]; 8 | printf("Enter %d numbers: ", n); 9 | for (i = 0; i < n; i++) 10 | scanf("%d", &a[i]); 11 | printf("Before sorting.\n"); 12 | for (i = 0; i < n; i++) 13 | printf("%d\t", a[i]); 14 | // sorting 15 | for (i = 0; i < n; i++) 16 | for (j = 0; j < n - 1 - i; j++) 17 | if (a[j] > a[j + 1]) 18 | { 19 | temp = a[j]; 20 | a[j] = a[j + 1]; 21 | a[j + 1] = temp; 22 | } 23 | // printing 24 | printf("\n"); 25 | printf("After sorting.\n"); 26 | for (i = 0; i < n; i++) 27 | printf("%d\t", a[i]); 28 | printf("\n"); 29 | } 30 | -------------------------------------------------------------------------------- /programs/record/exp20.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | FILE *fp; 7 | char ch[100], data[100]; 8 | printf("Enter contents to add in file: "); 9 | gets(ch); 10 | fp = fopen("out.txt", "w"); 11 | fprintf(fp, "%s", ch); 12 | fclose(fp); 13 | 14 | fp = fopen("out.txt", "r"); 15 | printf("Reading data: \n"); 16 | fgets(data, strlen(ch) + 1, fp); 17 | printf("%s", data); 18 | fclose(fp); 19 | 20 | fp = fopen("out.txt", "a"); 21 | printf("\nAppending to file: "); 22 | printf("\nEnter data: "); 23 | gets(ch); 24 | fprintf(fp, "%s", ch); 25 | fclose(fp); 26 | 27 | fp = fopen("out.txt", "r"); 28 | printf("Reading final data: \n"); 29 | fgets(data, 2 * (strlen(ch) + 1), fp); 30 | printf("%s", data); 31 | fclose(fp); 32 | } -------------------------------------------------------------------------------- /programs/tutorial2/19interest.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void main() 5 | { 6 | float p, t, r, si, ci; 7 | printf("Enter principal amount (p): "); 8 | scanf("%f", &p); 9 | printf("Enter time in year (t): "); 10 | scanf("%f", &t); 11 | printf("Enter rate in percent (r): "); 12 | scanf("%f", &r); 13 | 14 | int ch; 15 | printf("Options:\n1. Simple interest.\n2. Compound interest. \n\nEnter choice number: "); 16 | scanf("%d", &ch); 17 | switch (ch) 18 | { 19 | case 1: 20 | si = (p * t * r) / 100.0; 21 | printf("Simple Interest = %f\n", si); 22 | break; 23 | case 2: 24 | ci = p * (pow(1 + r / 100, t) - 1); 25 | printf("Compound Interest = %f", ci); 26 | break; 27 | default: 28 | printf("Wrong choice!\n"); 29 | return; 30 | } 31 | } -------------------------------------------------------------------------------- /programs/tutorial-3-8-22/q2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct Employee 4 | { 5 | char name[30]; 6 | int age; 7 | float bs; 8 | float da; 9 | float hra; 10 | float tsalary; 11 | }; 12 | 13 | void main() 14 | { 15 | struct Employee e; 16 | printf("Enter the name:"); 17 | scanf("%s", e.name); 18 | printf("Enter the age:"); 19 | scanf("%d", &e.age); 20 | printf("Enter the basic salary:"); 21 | scanf("%f", &e.bs); 22 | printf("Enter the da:"); 23 | scanf("%f", &e.da); 24 | printf("Enter the hra:"); 25 | scanf("%f", &e.hra); 26 | e.tsalary = (1 + e.da + e.hra) * e.bs; 27 | printf("Name=%s\n", e.name); 28 | printf("Age=%d\n", e.age); 29 | printf("Basic Salary=%.2f\n", e.bs); 30 | printf("DA=%.2f\n", e.da); 31 | printf("HRA=%.2f\n", e.hra); 32 | printf("Total Salary=%.2f\n", e.tsalary); 33 | } -------------------------------------------------------------------------------- /programs/struct/struct1.c: -------------------------------------------------------------------------------- 1 | // WAP in C to store the information of N students using Array 2 | 3 | #include 4 | struct student 5 | { 6 | char name[60]; 7 | int roll; 8 | float marks; 9 | }; 10 | struct student s[10]; 11 | 12 | int main() { 13 | int i, n; 14 | printf("Enter the number of students: "); 15 | scanf("%d", &n); 16 | printf("Enter information of students: \n"); 17 | for(i=0; i 2 | struct Student 3 | { 4 | char name[30]; 5 | int rollnum; 6 | int mark_for_C; 7 | }; 8 | void main() 9 | { 10 | struct Student s[30]; 11 | int i, n, sum = 0; 12 | float avg; 13 | printf("Enter the no of Student: "); 14 | scanf("%d", &n); 15 | for (i = 0; i < n; i++) 16 | { 17 | printf("Enter the Student name: "); 18 | scanf("%s", &s[i].name); 19 | printf("Enter the Student rollnum: "); 20 | scanf("%d", &s[i].rollnum); 21 | printf("Enter the Student Mark for C:"); 22 | scanf("%d", &s[i].mark_for_C); 23 | } 24 | printf("Name\tRoll Number\tMark for C\n"); 25 | for (i = 0; i < n; i++) 26 | { 27 | printf("%s\t%d\t%d\n", s[i].name, s[i].rollnum, s[i].mark_for_C); 28 | sum = sum + s[i].mark_for_C; 29 | } 30 | avg = sum / (float)n; 31 | printf("Average Mark = %.2f\n", avg); 32 | } -------------------------------------------------------------------------------- /programs/record/exp14.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void main() 4 | { 5 | char a[100]; 6 | int ct1 = 0, ct2 = 0, ct3 = 0, c = 0; 7 | printf("enter the string\n"); 8 | scanf("%[^\n]s", a); 9 | int l = strlen(a); 10 | if (a[l - 1] == '$') 11 | { 12 | for (int i = 0; a[i] != '$'; i++) 13 | if (a[i] == 'A' || a[i] == 'E' || a[i] == 'I' || a[i] == 'O' || a[i] == 'U' || a[i] == 'a' || a[i] == 'e' || a[i] == 'i' || a[i] == 'o' || a[i] == 'u') 14 | ct1++; 15 | else if (a[i] == '$') 16 | c++; 17 | else if (a[i] == ' ') 18 | ct2++; 19 | else 20 | ct3++; 21 | printf("The number of vowels is %d\n", ct1); 22 | printf("The number of space is %d\n", ct2); 23 | printf("The number of consonents is %d\n", ct3); 24 | } 25 | else 26 | printf("the string doesn't end with $"); 27 | } -------------------------------------------------------------------------------- /programs/struct/struct2.c: -------------------------------------------------------------------------------- 1 | // WAP in C to store the information of N books using array 2 | 3 | #include 4 | struct books 5 | { 6 | char name[60]; 7 | int serial; 8 | char author[50]; 9 | }; 10 | struct books s[10]; 11 | 12 | int main() { 13 | int i, n; 14 | printf("Enter the number of Books: "); 15 | scanf("%d", &n); 16 | printf("Enter information of Books:"); 17 | for(i=0; i 7 | 8 | struct employee 9 | { 10 | int id; 11 | char name[20]; 12 | float salary; 13 | }; 14 | 15 | void main() 16 | { 17 | int i, n, j; 18 | struct employee e[100], t; 19 | printf("Enter number of employees: "); 20 | scanf("%d", &n); 21 | for (i = 0; i < n; i++) 22 | { 23 | printf("Enter Employee ID, Name and Salary of Employee %d:\n", i + 1); 24 | scanf("%d %s %f", &e[i].id, &e[i].name, &e[i].salary); 25 | } 26 | 27 | printf("Sorting...\n"); 28 | for (i = 0; i < n; i++) 29 | for (j = 0; j < n - 1 - i; j++) 30 | if (e[j].salary > e[j + 1].salary) 31 | { 32 | t = e[j]; 33 | e[j] = e[j + 1]; 34 | e[j + 1] = t; 35 | } 36 | 37 | printf("Sorted.\n"); 38 | printf("\t\tEmployee Details\n"); 39 | printf("ID\t\tName\t\tSalary\n"); 40 | for (i = 0; i < n; i++) 41 | printf("%d\t\t%s\t\t%f\n", e[i].id, e[i].name, e[i].salary); 42 | printf("\n"); 43 | } -------------------------------------------------------------------------------- /programs/tutorial2/11roots.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() 4 | { 5 | float a, b, c; 6 | float root1, root2, img, disc; 7 | printf("Enter value of 'a' of quadratic equation (aX^2 + bX + c): "); 8 | scanf("%f", &a); 9 | printf("Enter value of 'b' of quadratic equation (aX^2 + bX + c): "); 10 | scanf("%f", &b); 11 | printf("Enter values of 'c' of quadratic equation (aX^2 + bX + c): "); 12 | scanf("%f", &c); 13 | disc = (b * b) - (4 * a * c); 14 | switch (disc > 0) 15 | { 16 | case 1: 17 | root1 = (-b + sqrt(disc)) / (2 * a); 18 | root2 = (-b - sqrt(disc)) / (2 * a); 19 | printf("Two distinct and real roots exists: %.2f and %.2f", 20 | root1, root2); 21 | break; 22 | case 0: 23 | switch (disc < 0) 24 | { 25 | case 1: 26 | root1 = root2 = -b / (2 * a); 27 | img = sqrt(-disc) / (2 * a); 28 | printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f", 29 | root1, img, root2, img); 30 | break; 31 | case 0: 32 | // If disc is zero 33 | root1 = root2 = -b / (2 * a); 34 | printf("Two equal and real roots exists: %.2f and %.2f", root1, root2); 35 | break; 36 | } 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /programs/record/exp18.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | struct student 4 | { 5 | char name[100]; 6 | int roll_no; 7 | float mark_maths; 8 | float mark_chem; 9 | float mark_phy; 10 | float total_mark; 11 | }; 12 | 13 | int main(void) 14 | { 15 | int n, i, j; 16 | struct student t; 17 | printf("Enter the number of students\n"); 18 | scanf("%d", &n); 19 | struct student stu[n]; 20 | for (i = 0; i < n; i++) 21 | { 22 | printf("Enter the name of the student : "); 23 | scanf("%s", stu[i].name); 24 | printf("\nRoll_No : "); 25 | scanf("%d", &stu[i].roll_no); 26 | printf("\nEnter the marks of Physics Maths Chemistry : "); 27 | scanf("%f %f %f", &stu[i].mark_phy, &stu[i].mark_maths, &stu[i].mark_chem); 28 | stu[i].total_mark = (stu[i].mark_phy + stu[i].mark_maths + stu[i].mark_chem); 29 | } 30 | 31 | for (i = 0; i < n - 1; i++) 32 | for (j = 0; j < (n - 1) - i; j++) 33 | if (stu[j].total_mark < stu[j + 1].total_mark) 34 | { 35 | t = stu[j]; 36 | stu[j] = stu[j + 1]; 37 | stu[j + 1] = t; 38 | } 39 | 40 | printf("Students Rank published \n"); 41 | printf("_________________________________\n"); 42 | 43 | for (i = 0; i < n; i++) 44 | printf("Name : %s MARK : %f RANK : %d\n", stu[i].name, stu[i].total_mark, i + 1); 45 | } 46 | -------------------------------------------------------------------------------- /programs/exam/prime_arm_pref_odd_even.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void prime(int n) 4 | { 5 | int i, c = 0; 6 | for (i = 1; i < n; i++) 7 | if (n % i == 0) 8 | c++; 9 | if (c == 2) 10 | printf("%d is a prime number.\n", n); 11 | else 12 | printf("%d is not a prime number.\n", n); 13 | } 14 | 15 | void armstrong(int n) 16 | { 17 | int i, sum = 0, len = 0, temp = n, temp2 = n, d; 18 | while (temp > 0) 19 | { 20 | len++; 21 | temp /= 10; 22 | } 23 | while (temp2 > 0) 24 | { 25 | d = temp2 % 10; 26 | sum += pow(d, len); 27 | temp2 /= 10; 28 | } 29 | if (sum == n) 30 | printf("%d is an Armstrong number.\n", n); 31 | else 32 | printf("%d is not an Armstrong number.\n", n); 33 | } 34 | 35 | void perfect(int n) 36 | { 37 | int i, sum = 0; 38 | for (i = 1; i < n; i++) 39 | if (n % i == 0) 40 | sum += i; 41 | if (sum == n) 42 | printf("%d is a perfect number.\n", n); 43 | else 44 | printf("%d is not a perfect number.\n", n); 45 | } 46 | 47 | void odd_even(int n) 48 | { 49 | if (n % 2 == 0) 50 | printf("%d is an even number.\n", n); 51 | else 52 | printf("%d is an odd number.\n", n); 53 | } 54 | 55 | void main() 56 | { 57 | int n; 58 | printf("Enter a number: "); 59 | scanf("%d", &n); 60 | prime(n); 61 | armstrong(n); 62 | perfect(n); 63 | odd_even(n); 64 | } -------------------------------------------------------------------------------- /programs/stack/stack.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define Size 100 3 | int Top=-1, inpoot[Size]; 4 | void Push(); 5 | void Pop(); 6 | void Show(); 7 | void Peek(); 8 | 9 | int main() 10 | { 11 | int choice; 12 | while(1) { 13 | printf("\nOperations performed by Stack"); 14 | printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.Peek\n5.End"); 15 | printf("\n\nEnter the choice: "); 16 | scanf("%d",&choice); 17 | switch(choice) 18 | { 19 | case 1: Push(); 20 | break; 21 | case 2: Pop(); 22 | break; 23 | case 3: Show(); 24 | break; 25 | case 4: Peek(); 26 | break; 27 | case 5: return (0); 28 | 29 | default: printf("\nInvalid choice"); 30 | } 31 | } 32 | } 33 | void Push() { 34 | int x; 35 | if(Top==Size-1) { 36 | printf("\nOverflow\n"); 37 | } 38 | else { 39 | printf("\nEnter element to be inserted to the stack: "); 40 | scanf("%d",&x); 41 | Top=Top+1; 42 | inpoot[Top]=x; 43 | } 44 | } 45 | void Pop() { 46 | if(Top==-1) { 47 | printf("\nUnderflow\n"); 48 | } 49 | else { 50 | printf("\nPopped element: %d",inpoot[Top]); 51 | Top=Top-1; 52 | } 53 | } 54 | void Show() { 55 | if(Top==-1) { 56 | printf("\nUnderflow\n"); 57 | } 58 | else { 59 | printf("\nElements present in the stack: \n"); 60 | for(int i=Top;i>=0;--i) 61 | printf("%d\n",inpoot[i]); 62 | } 63 | } 64 | void Peek() { 65 | if(Top==-1) { 66 | printf("\nUnderflow\n"); 67 | } 68 | else { 69 | printf("\nThe Topmost element of the stack is: "); 70 | printf("%d\n", inpoot[Top]); 71 | } 72 | } -------------------------------------------------------------------------------- /programs/others/array.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main(void) 3 | { 4 | int ch, m, n, p, q, k, i, j; 5 | printf("Enter the number of rows and columns in the first array\n"); 6 | scanf("%d %d", &m, &n); 7 | printf("Enter the number of rows and columns in the second array\n"); 8 | scanf("%d %d", &p, &q); 9 | int a[m][n]; 10 | int b[p][q]; 11 | int c[m][q]; // for multiplication 12 | printf("Enter %d elements of the First array\n", m * n); 13 | for (i = 0; i < m; i++) 14 | for (j = 0; j < n; j++) 15 | scanf("%d", &a[i][j]); 16 | printf("Enter %d elements of the second array\n", p * q); 17 | for (i = 0; i < p; i++) 18 | for (j = 0; j < q; j++) 19 | scanf("%d", &b[i][j]); 20 | printf("Menu:\n1. find the sum of two matrices\n2. find the product of two matrices\n3. find the transpose of a matrix\n4. display a matrix.\n\nEnter your choice: "); 21 | scanf("%d", &ch); 22 | switch (ch) 23 | { 24 | case 1: 25 | if (!(m == p && n == q)) 26 | { 27 | printf("Addition is not possible!"); 28 | return; 29 | } 30 | printf("Sum of the arrays:\n"); 31 | for (i = 0; i < m; i++) 32 | { 33 | for (j = 0; j < n; j++) 34 | printf("%d ", (a[i][j] + b[i][j])); 35 | printf("\n"); 36 | } 37 | break; 38 | case 2: 39 | if (!(n == p)) 40 | { 41 | printf("Multiplication is not possible!"); 42 | return; 43 | } 44 | for (i = 0; i < m; i++) 45 | { 46 | for (j = 0; j < q; j++) 47 | { 48 | c[i][j] = 0; 49 | for (k = 0; k < p; k++) 50 | c[i][j] += a[i][k] * b[k][j]; 51 | } 52 | } 53 | printf("Product of the arrays:\n"); 54 | for (i = 0; i < m; i++) 55 | { 56 | for (j = 0; j < q; j++) 57 | printf("%d ", (c[i][j])); 58 | printf("\n"); 59 | } 60 | break; 61 | case 3: 62 | printf("The transpose of the first matrix is:\n"); 63 | for (i = 0; i < n; i++) 64 | { 65 | for (j = 0; j < m; j++) 66 | printf("%d ", a[j][i]); 67 | printf("\n"); 68 | } 69 | printf("The transpose of the second matrix is:\n"); 70 | for (i = 0; i < p; i++) 71 | { 72 | for (j = 0; j < q; j++) 73 | printf("%d ", b[j][i]); 74 | printf("\n"); 75 | } 76 | break; 77 | case 4: 78 | printf("Elements of the first array:\n"); 79 | for (i = 0; i < m; i++) 80 | { 81 | for (j = 0; j < n; j++) 82 | printf("%d ", a[i][j]); 83 | printf("\n"); 84 | } 85 | printf("Elements of the second array:\n"); 86 | for (i = 0; i < p; i++) 87 | { 88 | for (j = 0; j < q; j++) 89 | printf("%d ", b[i][j]); 90 | printf("\n"); 91 | } 92 | break; 93 | default: 94 | printf("Invalid choice!"); 95 | } 96 | } -------------------------------------------------------------------------------- /programs/record/exp11.c: -------------------------------------------------------------------------------- 1 | #include 2 | void main(void) 3 | { 4 | int ch, m, n, p, q, k, i, j; 5 | printf("Enter the number of rows and columns in the first array\n"); 6 | scanf("%d %d", &m, &n); 7 | printf("Enter the number of rows and columns in the second array\n"); 8 | scanf("%d %d", &p, &q); 9 | int a[m][n]; 10 | int b[p][q]; 11 | int c[m][q]; // for multiplication 12 | printf("Enter %d elements of the First array\n", m * n); 13 | for (i = 0; i < m; i++) 14 | for (j = 0; j < n; j++) 15 | scanf("%d", &a[i][j]); 16 | printf("Enter %d elements of the second array\n", p * q); 17 | for (i = 0; i < p; i++) 18 | for (j = 0; j < q; j++) 19 | scanf("%d", &b[i][j]); 20 | printf("Menu:\n1. find the sum of two matrices\n2. find the product of two matrices\n3. find the transpose of a matrix\n4. display a matrix.\n\nEnter your choice: "); 21 | scanf("%d", &ch); 22 | switch (ch) 23 | { 24 | case 1: 25 | if (!(m == p && n == q)) 26 | { 27 | printf("Addition is not possible!"); 28 | return; 29 | } 30 | printf("Sum of the arrays:\n"); 31 | for (i = 0; i < m; i++) 32 | { 33 | for (j = 0; j < n; j++) 34 | printf("%d ", (a[i][j] + b[i][j])); 35 | printf("\n"); 36 | } 37 | break; 38 | case 2: 39 | if (!(n == p)) 40 | { 41 | printf("Multiplication is not possible!"); 42 | return; 43 | } 44 | for (i = 0; i < m; i++) 45 | { 46 | for (j = 0; j < q; j++) 47 | { 48 | c[i][j] = 0; 49 | for (k = 0; k < p; k++) 50 | c[i][j] += a[i][k] * b[k][j]; 51 | } 52 | } 53 | printf("Product of the arrays:\n"); 54 | for (i = 0; i < m; i++) 55 | { 56 | for (j = 0; j < q; j++) 57 | printf("%d ", (c[i][j])); 58 | printf("\n"); 59 | } 60 | break; 61 | case 3: 62 | printf("The transpose of the first matrix is:\n"); 63 | for (i = 0; i < n; i++) 64 | { 65 | for (j = 0; j < m; j++) 66 | printf("%d ", a[j][i]); 67 | printf("\n"); 68 | } 69 | printf("The transpose of the second matrix is:\n"); 70 | for (i = 0; i < p; i++) 71 | { 72 | for (j = 0; j < q; j++) 73 | printf("%d ", b[j][i]); 74 | printf("\n"); 75 | } 76 | break; 77 | case 4: 78 | printf("Elements of the first array:\n"); 79 | for (i = 0; i < m; i++) 80 | { 81 | for (j = 0; j < n; j++) 82 | printf("%d ", a[i][j]); 83 | printf("\n"); 84 | } 85 | printf("Elements of the second array:\n"); 86 | for (i = 0; i < p; i++) 87 | { 88 | for (j = 0; j < q; j++) 89 | printf("%d ", b[i][j]); 90 | printf("\n"); 91 | } 92 | break; 93 | default: 94 | printf("Invalid choice!"); 95 | } 96 | } 97 | --------------------------------------------------------------------------------