Subtraction of matrix in c is a program to add two matrices.

What is matrix: It is a collection of numbers arranged in rows and columns.
Matrix representation
Matrices are stored in a multidimensional array in c which is stored in rows and column format.

How to subtract two matrices
Subtraction of matrix in c: First we take two matrices with specific rows and columns. Then we subtract the pair of entries by row index and column index. And store in the third matrix. Print Subtraction of matrices.
Note:- we can’t subtract matrices with different sizes.

Matrix Subtraction example

Matrix Subtraction

Algorithm subtraction of matrix in c

  1. Declare variable
  2. Take two matrices input
  3. Subtract matrices by adding it’s pair
  4. Print addition of matrices

Program for subtraction of matrix in c

#include<stdio.h>
main()
{
int i,j,rows,col;
printf("Enter number of rows\n");
scanf("%d",&rows);
printf("Enter number of columns\n");
scanf("%d",&col);

int a1[rows][col],a2[rows][col],sub[rows][col];
//Taking input for 1st matrix
printf("Enter Matrix 1\n");
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a1[i][j]);
}
}
//Taking input for 2nd matrix
printf("Enter Matrix 2\n");
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a2[i][j]);
}
}
//Subtraction of matrix
for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
sub[i][j]=a1[i][j]-a2[i][j];
}
}

printf("Subtraction of above matrices is\n");

for(i=0;i<rows;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",sub[i][j]);
}

printf("\n");
}

}

Output :

Subtraction of matrix in c

Write A Comment