C Program Subtract Two Matrices

By | March 13, 2020


C Program Subtract Two Matrices

C Program for Matrix Subtraction

C Program for Matrix Subtraction

/* C program to subtract two matrices

www.easycodebook.com
*/


#include <stdio.h>
 
int main()
{
   int m, n, i, j, matrix1[10][10], matrix2[10][10], subtraction[10][10];
 
   printf("Enter the number of rows and columns of matrix\n");
   scanf("%d%d", &m, &n);
   printf("Please Enter %d elements of first matrix\n",m*n);
 
   for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
         scanf("%d", &matrix1[i][j]);
 
   printf("Please Enter the %d elements of second matrix\n",m*n);
 
   for (i = 0; i < m; i++)
      for (j = 0 ; j < n; j++)
         scanf("%d", &matrix2[i][j]);
   
   printf("Subtraction Result of the two matrices is as follows:\n");
   
   for (i = 0; i < m; i++) {
      for (j = 0 ; j < n; j++) {
         subtraction[i][j] = matrix1[i][j] - matrix2[i][j];
         printf("%d\t", subtraction[i][j]);
      }
      printf("\n");
   }
 
   return 0;
}

Output

Enter the number of rows and columns of matrix
2
2
Please Enter 4 elements of first matrix
10
23
11
70
Please Enter the 4 elements of second matrix
1
4
7
3
Subtraction Result of the two matrices is as follows:
9         19
4         67

We can subtract two matrices, if they are both of the same order. That is they have same number of rows and columns.

We cannot subtract two matrices with different dimentions.