Write a program to implement Addition of two matrices.
#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter the elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter the element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter the elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}
return 0;
}
Output
Enter the elements of 1st matrix:
Enter the element a11: 12
Enter the element a12: 13
Enter the element a13: 14
Enter the element a21: 15
Enter the element a22: 16
Enter the element a23: 17
Enter the element a31: 18
Enter the element a32: 19
Enter the element a33: 20
Enter the elements of 2nd matrix:
Enter element b11: 21
Enter element b12: 22
Enter element b13: 23
Enter element b21: 24
Enter element b22: 25
Enter element b23: 26
Enter element b31: 27
Enter element b32: 28
Enter element b33: 29
Sum of two matrices:
33 35 37
39 41 43
45 47 49