Menus

Thursday 23 April 2020

Program to add two matrices in C


#include<stdio.h>

void main()
{

int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q;

printf("Enter the rows and column of first matrix : ");
scanf("%d%d",&m,&n);

printf("Enter the rows and column of second matrix : ");
scanf("%d%d",&p,&q);

if((m==p) && (n==q))
{

printf("Enter first matrix \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
 scanf("%d",&a[i][j]);
}
}


printf("Enter second 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];
}
}


printf("Sum of matrix : \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
 printf(" %d",c[i][j]);
}
printf("\n");
}


}
else
{
  printf("\nInvalid rows and columns\n");
}

}








No comments:

Post a Comment