In this post we will implement substraction of two matrices with C programming.
See the image below to understand,
PROGRAM :
See the image below to understand,
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,rows1,rows2,columns1,columns2;
printf("Enter order of matrix 1 :t");
scanf("%d%d",&rows1,&columns1);
printf("Enter order of matrix 2 :t");
scanf("%d%d",&rows2,&columns2);
int matrix1[rows1][columns1],matrix2[rows2][columns2],result[rows1][columns1];
if(rows1 != rows2 || columns1 != columns2 )
{
printf("Both matrices must have same of order ");
return 0;
}
printf("nEnter elements of matrix 1:n");
for(i=0;i<rows1;i++)
{
for(j=0;j<columns1;j++)
{
printf("Enter element m%d%d: ",i+1,j+1);
scanf("%d",&matrix1[i][j]);
}
}
printf("nEnter elements of matrix 2:n");
for(i=0;i<rows2;i++)
{
for(j=0;j<columns2;j++)
{
printf("Enter element m%d%d: ",i+1,j+1);
scanf("%d",&matrix2[i][j]);
}
}
printf("nMATRIX 1 is n");
for (i = 0; i < rows1; i++)
{
for (j = 0; j < columns1; j++)
{
printf("%dt", matrix1[i][j]);
}
printf("n");
}
printf("nMATRIX 2 is n");
for (i = 0; i < rows2; i++)
{
for (j = 0; j < columns2; j++)
{
printf("%dt", matrix2[i][j]);
}
printf("n");
}
for (i = 0; i < rows1; i++)
{
for (j = 0; j < columns1; j++)
{
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
printf("nThe Difference of two Matrices is : n");
for (i = 0; i < rows1; i++)
{
for (j = 0; j < columns1; j++)
{
printf("%dt", result[i][j]);
}
printf("n");
}
return 0;
}
OUTPUT :