You are here

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.

C programming code

  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.    int m, n, c, d, first[10][10], second[10][10], difference[10][10];
  6.  
  7.    printf("Enter the number of rows and columns of matrix\n");
  8.    scanf("%d%d", &m, &n);
  9.    printf("Enter the elements of first matrix\n");
  10.  
  11.    for (c = 0; c < m; c++)
  12.      for (d = 0 ; d < n; d++)
  13.        scanf("%d", &first[c][d]);
  14.  
  15.    printf("Enter the elements of second matrix\n");
  16.  
  17.    for (c = 0; c < m; c++)
  18.      for (d = 0; d < n; d++)
  19.          scanf("%d", &second[c][d]);
  20.  
  21.    printf("Difference of entered matrices:-\n");
  22.  
  23.    for (c = 0; c < m; c++) {
  24.      for (d = 0; d < n; d++) {
  25.        difference[c][d] = first[c][d] - second[c][d];
  26.        printf("%d\t",difference[c][d]);
  27.      }
  28.      printf("\n");
  29.    }
  30.  
  31.    return 0;
  32. }

Download Subtract matrices program.

Output of program:
Subtract matrices c program