-->

Thursday, May 29, 2025

c program to print sum of two matrices of order mxn | 2D Array-matrix examples

Array programs in C 


/* to find sum of two matrix of order mxn

*/

#include<stdio.h>

int main()

{

int m,n,i,j;

printf("enter m as rows and n as columns\n");

scanf("%d%d",&m,&n);//if m=3 then the loop would run for 0,1,2

int m1[m][n],m2[m][n];

printf("enter elements for first matrix m1\n");

for(i=0;i<=m-1;i++)

{

for(j=0;j<=n-1;j++)

{

printf("enter element for location %d %d\n",i,j);

scanf("%d",&m1[i][j]);

}

}

printf("enter elements for second matrix m2\n");

for(i=0;i<=m-1;i++)

{

for(j=0;j<=n-1;j++)

{

printf("enter element for location %d %d\n",i,j);

scanf("%d",&m2[i][j]);

}

}

printf("the sum of two matrices is:\n");

for(i=0;i<=m-1;i++)

{

for(j=0;j<=n-1;j++)

{

printf("%d",m1[i][j]+m2[i][j]);

}

printf("\n");

}

return 0;

}


Output:-

c program to find sum of two matrices



No comments:

Post a Comment