Sum of the two matrices in C Programming

ยท

1 min read

Sum of the two matrices in C Programming

#include<stdio.h>

int main(){

int m, n, i, j;

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

scanf("%d", &m);

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

scanf("%d", &n);

int mat1[m][n], mat2[m][n], sum[m][n];

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

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

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

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

}

}

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

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

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

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

}

}

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

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

sum[i][j] = mat1[i][j] + mat2[i][j];

}

}

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

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

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

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

}

printf("\n");

}

return 0;

}

Output :

Enter the number of rows: 2

Enter the number of columns: 2

Enter elements of first matrix: 1 2 1 2

Enter elements of second matrix: 1 2 1 2

Sum of the two matrices:

2 4

2 4

ย