Subtraction of two Matrices in C

Subtract matrices

C code to subtract matrices of any order. This program finds the difference between corresponding elements of two matrices and then print the resultant matrix.

#include<stdio.h>
#include<conio.h>

void main()
{
int m1[5][5],m2[5][5],sub[5][5];
int i,j,r1,c1,r2,c2;
printf(" enter the rows and column of matrix 1\n")';
scanf("%d%d",&r1,&c1);

printf("enter the elements of  matrix1\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}

printf("enter the rows and column of matrix 2\n");
scanf("%d%d",&r2,&c2)
printf("enter the elements of matrix 2\n");

for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",m2[i][j]);
}
}

if(r1==r2&&c1==c2)
{
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
sum[i][j]=m1[i][j]-m2[i][j];
}
}
}
else
{
printf(" Subtraction  not found");
}
printf("addition of two matrices\n");

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d",&sub[i][j]);
}
printf("\n");
}
getch();
}

Output:-




No comments:

Post a Comment