C Program to subtract two matrix
Program
#include<stdio.h>
#include<stdlib.h>
void main()
{
int mat1[10][10], mat2[10][10], res[10][10];
int row1, col1, row2, col2;
int i, j;
printf("Enter the number of rows of matrix 1 : ");
scanf("%d", &row1);
printf("Enter the number of cols of matrix 1 : ");
scanf("%d", &col1);
printf("Enter the number of rows of matrix 2 : ");
scanf("%d", &row2);
printf("Enter the number of cols of matrix 2 : ");
scanf("%d", &col2);
if (row1 != row2 || col1 != col2)
{
printf("Order of two matrices is not same\n");
exit(0);
}
printf("Enter %d elements for matrix 1:\n", row1 * col1);
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
scanf("%d", &mat1[i][j]);
}
}
printf("Enter %d elements for matrix 2:\n", row2 * col2);
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
scanf("%d", &mat2[i][j]);
}
}
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
res[i][j] = mat1[i][j] - mat2[i][j];
}
}
printf("The result of subtraction of two matrices is :\n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf("%d\t", res[i][j]);
}
printf("\n");
}
}
Output 1
Enter the number of rows of matrix 1 : 2
Enter the number of cols of matrix 1 : 2
Enter the number of rows of matrix 2 : 2
Enter the number of cols of matrix 2 : 2
Enter 4 elements for matrix 1:
34
22
12
86
Enter 4 elements for matrix 2:
4
12
22
77
The result of subtraction of two matrices is :
30 10
-10 9
Output 2
Enter the number of rows of matrix 1 : 3
Enter the number of cols of matrix 1 : 3
Enter the number of rows of matrix 2 : 4
Enter the number of cols of matrix 2 : 5
Order of two matrices is not same