Wednesday, April 8, 2015

29. Write a program to add two matrices.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,A[l0][10],B[10][10],sum[10][10],r1,c1,r2,c2;
clrscr();
printf("\n Input order of first matrix:-");
scanf("%d%d",&rl,&cl);
printf("\n Input order of second matrix:-");
scanf("%d%d"f&r2,&c2);
if(r1==r2 && cl==c2)

{
printf("\n Input Elements of First %d X %d Matrixs:- ",rl,cl);
for(i=0;i<rl;i++)
{
for(j=0;j<cl;j++)
{
printf("\n Elements [%d][%d]:- ",i, j);

scanf("%d", &A[i][j]);
}
}
printf("\n Input Elements of Second %d X %d Matrix:~ ",r2,c2);
for(i=0;i<r2;i++)

{
for(j=0;j<c2;j++)
{
printf("\n Elements [%d][%d]:- ",i, j);

scanf("%d", &B[i][j]);
}
}
for(i=0;i<rl;i++)
{
for(j=0;j<cl;j++) 

sum[i][j] = A[i][j] + B[i][j];
}
Printf("\n Addition of the Matrix: \n\n");
for(i=0;i<rl;i++)
{
for(j=0;j<c1;j++)

printf("%5d",sum[i][j]);
printf("\n“);
}
}
else
printf("\n Invalid Matrices");

getch();  
}

No comments:

Post a Comment