Tuesday, September 15, 2015

Filled Under:

C program to add two matrices

Share
#include<stdio.h>
#include<conio.h>
void main()
{
 int a[10][10], b[10][10],c[10][10],i,j,m,n,p,q;
clrscr();
printf("Enter the number of rows of matrix1:\n");
scanf("%d",&m);
printf("Enter the number of columns of matrix1:\n");
scanf("%d",&n);
printf("Enter the elements of matrix1:\n");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
printf("Enter the number of rows of matrix2:\n");
scanf("%d",&p);
printf("Enter the number of columns of matrix2:\n");
scanf("%d",&q);
if(m==p&&n==q)
{
printf(""Enter the  elements of matrix2:\n);
for(i=0;i<p;i++)
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
printf("Sum is:\n");
for(i=0;i<m;i++)
for(j=0;j<p;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",c[i][j]);
}
}
}
else
printf("Addition not possible");
getch();
}

0 comments:

Post a Comment