The Program in C to read two 3*3 matrix and add their values into third matrix is given below:
#include <stdio.h>
int main() {
int a[3][3], b[3][3], c[3][3], i, j;
printf("Enter elements of first 3x3 matrix: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of second 3x3 matrix: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
printf("Sum of the matrices: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
Output:
Enter elements of first 3×3 matrix:
3
6
8
9
3
5
1
2
4
Enter elements of second 3×3 matrix:
5
7
5
3
8
9
5
6
1
Sum of the matrices:
8 13 13
12 11 14
6 8 5
Pro-Tips:💡
This program uses two nested for loops to read the elements of the first 3×3 matrix from the user,
and another two nested loops to read the elements of the second 3×3 matrix.
Then, the program uses two nested for loops again to add the corresponding elements of the first and second matrices and store the result in the third matrix.
Finally, the program uses two nested for loops again to print the elements of the third matrix which is the sum of the first and second matrix.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.