Google Add

Search

Write a Program to Subtract Two Matrices in C


Like addition of two matrices, subtraction of two matrices will only be possible if both are of same size. I declare two matrices of 3 * 3 type.
I declare two matrices a and b , the result of matrices subtraction is stored in c. Here i declare matrix and initialize it's value without taking user input. If you want to take user input check my previous program on addition of two matrices.

Write a Program to Subtract Two Matrices in C



#include <stdio.h>

main()
{
   
   int a[3][3]={ {1,2,3},
                 {2,3,4},
                 {3,4,5}
                };
                
   int b[3][3]={ {4,3,1},
                 {2,5,8},
                 {3,7,2}
                };
                
   int c[3][3],i,j,k;
   
   for(i=0;i<3;i++){
       for(j=0;j<3;j++){
           c[i][j] = a[i][j] - b[i][j];
       }
   }
   
   
   printf("Subtraction of two matrix is \n");
   
   for(i=0;i<3;i++){
       for(j=0;j<3;j++){
           printf("%d \t",c[i][j]);
       }
       
       printf("\n");
   }
   
    
}

Output

Subtraction of two matrix is
-3 -1 2
0 -2 -4
0 -3 3

No comments:

Post a Comment