C Program Code:
#include<stdio.h>
int main()
{
int m1[2][2],m2[2][2],m3[2][2]; //Declare three 2 Dimensional Arrays
int i,j;
// Input First Matrix
printf("Enter first matrix of order 2x2 \n");
for(i=0; i<2; i++)
{
printf("Enter elements of %d row \n",i);
for(j=0; j<2; j++)
{
scanf("%d",&m1[i][j]);
}
}
// Input Second Matrix
printf("Enter second matrix of order 2*2 \n");
for(i=0; i<2; i++)
{
printf("Enter elements of %d row \n",i);
for(j=0; j<2; j++)
{
scanf("%d",&m2[i][j]);
}
}
// Adding two matrices by adding the corresponding entries together and store in 3rd Matrix
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
}
}
printf("Addition Matrix \n");
// Print 3rd Matrix
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%d ",m3[i][j]);
}
printf("\n");
}
printf("Press enter to continue... \n");
fflush(stdin);
getchar();
return 0;
}
|
Matrix Addition in C Language Output Screenshot
C Program