0% found this document useful (0 votes)
25 views5 pages

C Programs for Matrix Operations and More

The document contains multiple C programs that demonstrate various programming concepts, including matrix operations (addition and transposition), array manipulations (finding sum, average, largest and smallest elements), sorting, character classification, and checking conditions. Each program is structured with input prompts, processing logic, and output statements. The programs cover a range of functionalities from basic arithmetic to more complex operations like checking for prime numbers and handling character cases.

Uploaded by

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

C Programs for Matrix Operations and More

The document contains multiple C programs that demonstrate various programming concepts, including matrix operations (addition and transposition), array manipulations (finding sum, average, largest and smallest elements), sorting, character classification, and checking conditions. Each program is structured with input prompts, processing logic, and output statements. The programs cover a range of functionalities from basic arithmetic to more complex operations like checking for prime numbers and handling character cases.

Uploaded by

gokasuresh20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Write a c program to add some of two matrixes by Write a c program to transpose given matrixes

using 2Darray. #include <stdio.h>


#include <stdio.h> int main() {
int main() { int a[3][3], transpose[2][2];
int a[2][2]; int i, j;
int b[2][2]; printf("Enter 3x3 matrix elements:\n");
int sum[2][2]; for(i = 0; i < 2; i++) {
int i, j; for(j = 0; j < 2; j++) {
printf("Enter elements of first matrix (2x2):\n"); scanf("%d", &a[i][j]);
for (i = 0; i < 2; i++) { }
for (j = 0; j < 2; j++) { }
scanf("%d", &a[i][j]); for(i = 0; i < 2; i++) {
} for(j = 0; j < 2; j++) {
} transpose[j][i] = a[i][j];
printf("Enter elements of second matrix (2x2):\n"); }
for (i = 0; i < 2; i++) { }
for (j = 0; j < 2; j++) { printf("Transpose of matrix:\n");
scanf("%d", &b[i][j]); for(i = 0; i < 2; i++) {
} for(j = 0; j < 2; j++) {
} printf("%d ", transpose[i][j]);
// Calculate sum }
for (i = 0; i < 2; i++) { printf("\n");
for (j = 0; j < 2; j++) { }
sum[i][j] = a[i][j] + b[i][j]; return 0;
} }
}
printf("Sum of matrices:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}

Find Sum and Average of Array Elements Find the Largest and Smallest Element in
#include <stdio.h> an Array
int main() { #include <stdio.h>
int n, i; int main() {
float sum = 0; int n, i, max, min;
printf("Enter number of elements: "); printf("Enter number of elements: ");
scanf("%d", &n); scanf("%d", &n);
int arr[n];
int arr[n]; printf("Enter elements: ");
printf("Enter elements: "); for(i = 0; i < n; i++) scanf("%d", &arr[i]);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]); max = min = arr[0];
sum += arr[i]; for(i = 1; i < n; i++) {
} if(arr[i] > max) max = arr[i];
if(arr[i] < min) min = arr[i];
printf("Sum = %.2f\n", sum); }
printf("Average = %.2f\n", sum / n); printf("Max = %d, Min = %d\n", max, min);
return 0; return 0;
} }
Write a program on Sort an Array C program to extract the last two
(Ascending Order) digits of a given integer n, where the
#include <stdio.h> number of digits should be greater
int main() { than 2.
int n, i, j, temp; #include <stdio.h>
printf("Enter number of elements: ");
scanf("%d", &n); int main() {
int arr[n]; int n, lastTwo;
printf("Enter elements: "); printf("Enter an integer (more than 2 digits):
for(i = 0; i < n; i++) scanf("%d", &arr[i]); ");
scanf("%d", &n);
for(i = 0; i < n - 1; i++) {
for(j = i + 1; j < n; j++) { if (n < 100 && n > -100) {
if(arr[i] > arr[j]) { printf("Number should have more than 2
temp = arr[i]; digits!\n");
arr[i] = arr[j]; } else {
arr[j] = temp; lastTwo = n % 100; // get remainder after
} dividing by 100
} if (lastTwo < 0) // handle negative
} numbers
lastTwo = -lastTwo;
printf("Sorted array: "); printf("Last two digits = %d\n", lastTwo);
for(i = 0; i < n; i++) printf("%d ", arr[i]); }
return 0;
} return 0;
}
C program to display the greatest of three C program to swap two numbers without
numbers using a conditional operator. using a third variable.
#include <stdio.h>
#include <stdio.h>
int main() {
int a, b, c, greatest; int main() {
int a, b;
printf("Enter three numbers: "); printf("Enter two numbers: ");
scanf("%d %d %d", &a, &b, &c); scanf("%d %d", &a, &b);
// Using conditional (ternary) operator
greatest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : printf("Before swapping: a = %d, b =
c); %d\n", a, b);

printf("The greatest number is: %d\n", greatest); // Swap without using a third variable
a = a + b;
return 0;
b = a - b;
}
a = a - b;
printf("After swapping: a = %d, b = %d\n",
a, b);

return 0;
}
C program to check whether a given input C program to check whether a given character is a
integer is in between two values x and y. vowel or a consonant or a digit or a Special symbol.
#include <stdio.h>
#include <stdio.h>
int main() {
int main() { char ch;
int x, y, n; printf("Enter a character: ");
printf("Enter two numbers (x and y): "); scanf("%c", &ch);
scanf("%d %d", &x, &y); if (ch >= '0' && ch <= '9') {
printf("Enter a number to check: "); printf("%c is a digit.\n", ch);
}
scanf("%d", &n);
else if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch
if (n > x && n < y) == 'u' ||
printf("%d is between %d and %d\n", n, ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch
x, y); == 'U') {
else printf("%c is a vowel.\n", ch);
printf("%d is NOT between %d and }
else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch
%d\n", n, x, y);
<= 'Z')) {
printf("%c is a consonant.\n", ch);
return 0; }
} else {
printf("%c is a special symbol.\n", ch);
}

return 0;
}
C program to display the nature of roots of a C program to convert upper case character to
quadratic equation. lowercase and vice versa.
#include <stdio.h> #include <stdio.h>
int main() { int main() {
float a, b, c, discriminant; char ch;

printf("Enter coefficients a, b, and c: "); printf("Enter a character: ");


scanf("%f %f %f", &a, &b, &c); scanf("%c", &ch);

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


printf("This is not a quadratic equation.\n"); ch = ch + 32;
return 0; printf("Lowercase: %c\n", ch);
} }
else if (ch >= 'a' && ch <= 'z') {
discriminant = b*b - 4*a*c; ch = ch - 32;
printf("Uppercase: %c\n", ch);
if (discriminant > 0) { }
printf("The roots are real and distinct.\n"); else {
} printf("%c is not an alphabet.\n", ch);
else if (discriminant == 0) { }
printf("The roots are real and equal.\n");
} return 0;
else { }
printf("The roots are complex and
imaginary.\n");
}

return 0;
}
C program to print odd numbers between specified C program to display the factors of a given number
ranges. and check whether it is a prime or not.
#include <stdio.h> #include <stdio.h>
int main() { int main() {
int start, end, i; int n, i, count = 0;

printf("Enter the starting number: "); printf("Enter a number: ");


scanf("%d", &start); scanf("%d", &n);

printf("Enter the ending number: "); printf("Factors of %d are: ", n);


scanf("%d", &end); for(i = 1; i <= n; i++) {
if(n % i == 0) {
printf("Odd numbers between %d and %d are: ", printf("%d ", i);
start, end); count++; // count factors
}
for(i = start; i <= end; i++) { }
if(i % 2 != 0) { // Check if number is odd
printf("%d ", i); printf("\n");
}
} if(count == 2)
printf("%d is a prime number.\n", n);
printf("\n"); else
return 0; printf("%d is not a prime number.\n", n);
}
return 0;
}

You might also like