0% found this document useful (0 votes)
2 views9 pages

All

The document contains multiple C programs that demonstrate various operations on matrices and arrays, including input, addition, multiplication, transposition, and summation of elements. Each section provides code snippets for specific functionalities, such as checking for prime numbers and calculating the sum of array elements. The programs are structured to take user input and display results accordingly.

Uploaded by

karandehrionsone
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)
2 views9 pages

All

The document contains multiple C programs that demonstrate various operations on matrices and arrays, including input, addition, multiplication, transposition, and summation of elements. Each section provides code snippets for specific functionalities, such as checking for prime numbers and calculating the sum of array elements. The programs are structured to take user input and display results accordingly.

Uploaded by

karandehrionsone
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
You are on page 1/ 9

User input in array

#include <stdio.h>
int main() {
int a[3][3]; // 2D array with max size 3*3
int rows, cols;

// Input elements
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("Enter element at [%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}

// Display elements
printf("\nThe 2D array is:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}

return 0;
}
// Print first matrix
Sum of two matrices(display k liye ek or printf("\nFirst Matrix:\n");
loop & print) for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
#include <stdio.h> printf("%d\t", a[i][j]);
}
int main() { printf("\n");
int a[3][3], b[3][3], sum[3][3]; }
int rows, cols;
// Input second matrix
// Input matrix size printf("\nEnter elements of second
printf("Enter number of rows (max 10): "); matrix:\n");
scanf("%d", &rows); for(int i = 0; i < rows; i++) {
printf("Enter number of columns (max 10): "); for(int j = 0; j < cols; j++) {
scanf("%d", &cols); printf("b[%d][%d] = ", i, j);
scanf("%d", &b[i][j]);
// Input first matrix }
printf("\nEnter elements of first matrix:\n"); }
for(int i = 0; i < rows; i++) { // Print second matrix
for(int j = 0; j < cols; j++) { printf("\nSecond Matrix:\n");
printf("a[%d][%d] = ", i, j); for(int i = 0; i < rows; i++) {
scanf("%d", &a[i][j]); for(int j = 0; j < cols; j++) {
} printf("%d\t", b[i][j]);
} }
printf("\n");
}
// Calculate sum of the two matrices
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}

// Display the result Here's an updated C program that:


printf("\nSum of the two 01.Takes input for two matrices
matrices:\n"); 02.Prints both matrices
for(int i = 0; i < rows; i++) { 03.Adds them
for(int j = 0; j < cols; j++) { 04.Prints the result
printf("%d\t", sum[i][j]);
}
printf("\n");
}

return 0;
}
Multiply
#include <stdio.h> // Input elements of first matrix
printf("\nEnter elements of first
int main() { matrix:\n");
int a[10][10], b[10][10], product[10][10]; for(int i = 0; i < r1; i++) {
int r1, c1, r2, c2; for(int j = 0; j < c1; j++) {
printf("a[%d][%d] = ", i, j);
// Input sizes of matrices scanf("%d", &a[i][j]);
printf("Enter rows and columns of first matrix: "); }
scanf("%d %d", &r1, &c1); }

printf("Enter rows and columns of second matrix: "); // Input elements of second matrix
scanf("%d %d", &r2, &c2); printf("\nEnter elements of second
matrix:\n");
// Check multiplication condition for(int i = 0; i < r2; i++) {
if (c1 != r2) { for(int j = 0; j < c2; j++) {
printf("Matrix multiplication not possible. Columns printf("b[%d][%d] = ", i, j);
of first must equal rows of second.\n"); scanf("%d", &b[i][j]);
return 0; }
} }
// Initialize product matrix to 0
for(int i = 0; i < r1; i++) {
for(int j = 0; j < c2; j++) {
product[i][j] = 0;
}
}

// Matrix multiplication logic


for(int i = 0; i < r1; i++) {
This version will:
for(int j = 0; j < c2; j++) {
for(int k = 0; k < c1; k++) { 01.Take input for two matrices.
product[i][j] += a[i][k] * b[k][j]; 02.Print both matrices before
} 03.performing multiplication.
} 04.Show the resulting product
} matrix.
// Display the result
printf("\nProduct of the two matrices:\n");
for(int i = 0; i < r1; i++) {
for(int j = 0; j < c2; j++) {
printf("%d\t", product[i][j]);
}
printf("\n");
}

return 0;
}
Transverse
#include <stdio.h>

int main() {
int a[10][10], transpose[10][10];
int rows, cols;

// Input matrix size


printf("Enter number of rows (max 10): ");
scanf("%d", &rows);
printf("Enter number of columns (max 10): ");
scanf("%d", &cols);

// Input elements of the matrix


printf("\nEnter elements of the matrix:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("a[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}
// Transpose the matrix (swap rows with columns)
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
transpose[j][i] = a[i][j];
}
}

// Print original matrix


printf("\nOriginal Matrix:\n");
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}

// Print transposed matrix


printf("\nTransposed Matrix:\n");
for(int i = 0; i < cols; i++) {
for(int j = 0; j < rows; j++) {
printf("%d\t", transpose[i][j]);
}
printf("\n");
}

return 0;
}
C Program to Find Sum of All Elements of an Array
include <stdio.h>

int main() {
int arr[100], n, sum = 0;

// Input number of elements in the array


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

// Input array elements


printf("Enter %d elements:\n", n);
for(int i = 0; i < n; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}

// Calculate the sum


for(int i = 0; i < n; i++) {
sum += arr[i]; // Adding each element to sum
}

// Output the result


printf("Sum of all elements in the array = %d\n", sum);

return 0;
}
PRIME OR NOT
#include <stdio.h>

int main() {
int num, i, count = 0;

printf("Enter a number: ");


scanf("%d", &num);

// Count number of divisors


for(i = 1; i <= num; i++) {
if(num % i == 0)
count++;
}

// Prime numbers have exactly 2 divisors: 1 and itself


if(count == 2)
printf("%d is a Prime Number.\n", num);
else
printf("%d is NOT a Prime Number.\n", num);

return 0;
}

You might also like