Write a program to add two matrices and store the result in the third matrix
#include<stdio.h> int main() { int a[5][5],b[5][5],c[5][5],row,column,n,m,p,q,i,j; printf("Enter the size of the 1st matrix.\n"); scanf("%d%d",&m,&n); printf("Enter the size of the 2nd matrix.\n"); scanf("%d%d",&p,&q); if(m==p&&n==q) { printf("Enter element in 1st matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } printf("Enter element in 2nd matrix:\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) scanf("%d",&b[i][j]); } for(i=0;i<m;i++){ for(j=0;j<n;j++){ c[i][j]=a[i][j]+b[i][j]; } } for(i=0;i<m;i++) { for(j=0;j<n;j++) printf("%d ",c[i][j]); } } return 0; }
Output
Enter the size of the 1st matrix.
2
2
Enter the size of the 2nd matrix.
2
2
Enter element in 1st matrix:
1
2
3
4
Enter element in 2nd matrix:
4
3
2
1
5 5 5 5