Write a C program to accept details of two matrices, Add given matrices and print the result.
#include <stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n;
printf("Enter the value of row size ");
scanf("%d",&m);
printf("Enter the value of col size ");
scanf("%d",&n);
printf("Enter first matrix\n"); //Accepting first matrix from user
for(i=0;i<m;i++) //first for loop for accessing row values
{
for(j=0;j<n;j++) //second for loop for accessing column values
{
scanf("%d",&a[i][j]);
}
printf("\n");
}
printf("Enter Second matrix\n"); //Accepting second matrix from user
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++) //Addition in third matrix
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
printf("\n");
}
printf("Addition in third matrix\n"); //displaying result
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
Output: Testcase-1
Enter the value of row size 3
Enter the value of col size 3
Enter first matrix
1 2 3
4 5 2
2 2 1
Enter Second matrix
2 2 1
1 0 2
3 2 4
Addition in third matrix
3 4 4
5 5 4
5 4 5
Output: Testcase-2
Enter the value of row size 2
Enter the value of col size 2
Enter first matrix
3 4
6 7
Enter Second matrix
1 2
3 4
Addition in third matrix
4 6
9 11
Comments