In this example, we will add two matrices in C programming using two-dimensional arrays.
#include <stdio.h>
int main()
{
int A[10][10],B[10][10],C[10][10],i,j,row,col;
printf("\n Enter row and column: ");
scanf("%d %d",&row,&col);
printf("\n Enter A matrix");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("\n Enter B matrix");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&B[i][j]);
}
}
printf("\n Sum of the two matrices: \n ");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
C[i][j]=A[i][j]+B[i][j];
printf("%d \t",C[i][j]);
}
printf("\n");
}
return 0;
}
Output :
Enter row and column: 2 3
Enter A matrix1
2
3
4
5
6
Enter B matrix1
2
3
4
5
6
Sum of the two matrices:
2 4 6
8 10 12
Ask anything about this examples