C program source code to Add two Matrices.
/*Add Two Matrices*/
/*mechatrofice*/
#include <stdio.h>
#include <conio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,m1,m2,n1,n2; clrscr(); printf("Matrix1\nEnter the no of rows="); scanf("%d",&m1); printf("Enter the no of columns="); scanf("%d",&n1); printf("Enter the matrix element\n"); for(i=0;i<m1;i++){ for(j=0;j<n1;j++){ scanf("%d",&a[i][j]); }} printf("Matrix2\nEnter the no of rows="); scanf("%d",&m2); if(m1==m2){ printf("Enter the no of columns="); scanf("%d",&n2); if(n1==n2){ printf("Enter the matrix element\n"); for(i=0;i<m2;i++){ for(j=0;j<n2;j++){ scanf("%d",&b[i][j]); }} printf("Matrix1\n"); for(i=0;i<m1;i++){ for(j=0;j<n1;j++){ printf("\t%d\t",a[i][j]); } printf("\n"); } printf("\t\t\t+\nMatrix2\n"); for(i=0;i<m2;i++){ for(j=0;j<n2;j++){ printf("\t%d\t",b[i][j]); } printf("\n"); } for(i=0;i<m1;i++){ for(j=0;j<n1;j++){ c[i][j]=a[i][j]+b[i][j]; }} printf("\t\t\t=\nMatrix sum\n"); for(i=0;i<m1;i++){ for(j=0;j<n1;j++){ printf("\t%d\t",c[i][j]); } printf("\n"); }} else{ printf("Dimension Error"); }} else{ printf("Dimension Error"); } getch(); } |
---|
Output