0% found this document useful (0 votes)
1 views3 pages

c code

code for c programs

Uploaded by

Om Naidu
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)
1 views3 pages

c code

code for c programs

Uploaded by

Om Naidu
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/ 3

Name: Polepalli Om Naidu

Roll no: HU22CSEN0101265

MATRIX MULTIPLICATION CODE:

#include <stdio.h>

int main() {
int rowsA, colsA, rowsB, colsB;
int matrixA[100][100], matrixB[100][100], result[100][100];
int row, col, index;

printf("Enter rows and columns for FIRST matrix: ");


scanf("%d%d", &rowsA, &colsA);

printf("Enter rows and columns for SECOND matrix: ");


scanf("%d%d", &rowsB, &colsB);

if (colsA != rowsB) {
printf("Matrix multiplication not possible.\n");
return 0;
}

printf("\nEnter elements of FIRST matrix:\n");


for (row = 0; row < rowsA; ++row)
for (col = 0; col < colsA; ++col) {
printf("Enter element A%d%d: ", row + 1, col + 1);
scanf("%d", &matrixA[row][col]);
}

printf("\nEnter elements of SECOND matrix:\n");


for (row = 0; row < rowsB; ++row)
for (col = 0; col < colsB; ++col) {
printf("Enter element B%d%d: ", row + 1, col + 1);
scanf("%d", &matrixB[row][col]);
}

// Initialize result matrix to 0


for (row = 0; row < rowsA; ++row)
for (col = 0; col < colsB; ++col)
result[row][col] = 0;
// Matrix multiplication
for (row = 0; row < rowsA; ++row)
for (col = 0; col < colsB; ++col)
for (index = 0; index < colsA; ++index)
result[row][col] += matrixA[row][index] * matrixB[index][col];

printf("\nProduct of the two matrices:\n");


for (row = 0; row < rowsA; ++row) {
for (col = 0; col < colsB; ++col) {
printf("%d ", result[row][col]);
}
printf("\n\n");
}

return 0;
}

MATRIX ADDITION:​

#include <stdio.h>

int main() {
int rows, cols;
int matrixA[100][100], matrixB[100][100], matrixSum[100][100];
int row, col;

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


scanf("%d", &rows);

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


scanf("%d", &cols);

printf("\nEnter elements of the FIRST matrix:\n");


for (row = 0; row < rows; ++row)
for (col = 0; col < cols; ++col) {
printf("Enter element A%d%d: ", row + 1, col + 1);
scanf("%d", &matrixA[row][col]);
}

printf("Enter elements of the SECOND matrix:\n");


for (row = 0; row < rows; ++row)
for (col = 0; col < cols; ++col) {
printf("Enter element B%d%d: ", row + 1, col + 1);
scanf("%d", &matrixB[row][col]);
}

for (row = 0; row < rows; ++row)


for (col = 0; col < cols; ++col) {
matrixSum[row][col] = matrixA[row][col] + matrixB[row][col];
}

printf("\nSum of the two matrices:\n");


for (row = 0; row < rows; ++row)
for (col = 0; col < cols; ++col) {
printf("%d ", matrixSum[row][col]);
if (col == cols - 1) {
printf("\n\n");
}
}

return 0;
}

- END -

You might also like