add two given matrix
#include<stdio.h>
#include<conio.h>
main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j;
clrscr();
printf("\n enter the no. of rows the matrix A:");
scanf("%d",&m);
printf("\n enter the no. of columns the matrix A:");
scanf("%d",&n);
printf("\n enter the no. of rows the matrix B:");
scanf("%d",&p);
printf("\n enter the no. of columns the matrix B:");
scanf("%d",&q);
if((m==p) && (n==q))
{
printf("\n The two matrices can be added ");
/* enter the matrix A */
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("\n enter the a[%d][%d] element:",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* enter the matric B */
for(i=0;i<p;i++)
for(j=0;j<q;j++)
{
printf("\n enter the b[%d][%d] element:",i+1,j+1);
scanf("%d",&b[i][j]);
}
clrscr();
/* printing the matric A */
printf("\n The elements of the given matrix A are:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t %d",a[i][j]);
}
/* printing the matric B */
printf("\n The elements of the given matrix B are:");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("\t %d",b[i][j]);
}
for(i=0;i<m;i++) /* adding the two matrices */
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
/* printing the addition of the given matrices */
printf("\n The addition of the given matrices are:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t %d",c[i][j]);
}
}
else
printf("\n The two matrices can not be added ");
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j;
clrscr();
printf("\n enter the no. of rows the matrix A:");
scanf("%d",&m);
printf("\n enter the no. of columns the matrix A:");
scanf("%d",&n);
printf("\n enter the no. of rows the matrix B:");
scanf("%d",&p);
printf("\n enter the no. of columns the matrix B:");
scanf("%d",&q);
if((m==p) && (n==q))
{
printf("\n The two matrices can be added ");
/* enter the matrix A */
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("\n enter the a[%d][%d] element:",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* enter the matric B */
for(i=0;i<p;i++)
for(j=0;j<q;j++)
{
printf("\n enter the b[%d][%d] element:",i+1,j+1);
scanf("%d",&b[i][j]);
}
clrscr();
/* printing the matric A */
printf("\n The elements of the given matrix A are:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t %d",a[i][j]);
}
/* printing the matric B */
printf("\n The elements of the given matrix B are:");
for(i=0;i<p;i++)
{
printf("\n");
for(j=0;j<q;j++)
printf("\t %d",b[i][j]);
}
for(i=0;i<m;i++) /* adding the two matrices */
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
/* printing the addition of the given matrices */
printf("\n The addition of the given matrices are:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t %d",c[i][j]);
}
}
else
printf("\n The two matrices can not be added ");
getch();
}
No comments:
Post a Comment