C Programming Code Examples C > Arrays and Matrices Code Examples C program to add two matrices C program to add two matrices Write a C program to read elements in two matrices and add elements of both matrices. C program for addition of two matrix. Matrix addition program in C. Logic to add two matrix in C programming. /** * C program to find sum of two matrices of size 3x3 */ #include <stdio.h> #define SIZE 3 // Size of the matrix int main() { int M1[SIZE][SIZE]; // Matrix 1 int M2[SIZE][SIZE]; // Matrix 2 int M3[SIZE][SIZE]; // Resultant matrix int row, col; /* Input elements in first matrix*/ printf("Enter elements in matrix M1 of size 3x3: \n"); for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { scanf("%d", &M1[row][col]); } } /* Input elements in second matrix */ printf("\nEnter elements in matrix M2 of size 3x3: \n"); for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { scanf("%d", &M2[row][col]); } } /* * Add both matrices M1 and M2 entry wise or element wise * and stores result in matrix M3 */ for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { /* M3ij = M1ij + M2ij */ M3[row][col] = M1[row][col] + M2[row][col]; } } /* Print the value of resultant matrix M3 */ printf("\nSum of matrices M1+M2 = \n"); for(row=0; row<SIZE; row++) { for(col=0; col<SIZE; col++) { printf("%d ", C[row][col]); } printf("\n"); } return 0; }