Write a c program to find the sum of two matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],s[10][10],i,j,r1,c1,r2,c2;
clrscr();
printf("Enter the order of the first matrix\n");
scanf("%d%d",&r1,&c1);
printf("Enter the order of the second matrix\n");
scanf("%d%d",&r2,&c2);
if((r1==r2)&&(c1==c2))
{
printf("Enter the elements of first matrix\n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of second matrix\n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
s[i][j]=a[i][j]+b[i][j];
printf("The resultant matrix is\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",s[i][j]);
printf("\n");
}
}
else
printf("Matrices are not compatible");
getch();
}
Output:
run 1:
Enter the order of the first matrix
2
2
Enter the order of the second matrix
2
2
Enter the elements of first matrix
1
2
3
4
Enter the elements of second matrix
5
6
7
8
The resultant matrix is
6 8
10 12
run 2:
Enter the order of the first matrix
2
2
Enter the order of the second matrix
2
3
Matrices are not compatible
No comments: