Program for subtraction of two matrices

In this program we compute subtraction of two matrices A and B which has same order. First we ask user to  enter order or size of matrix, then enter numbers for matrix A and B. In this elements of matrix B subtract from corresponding elements of matrix A . After that gives resultant matrix.

To understand the strategy, let us have  look at the dummy matrices. Suppose matrices have order 3×3.

Also Read: Program for multiplication of two matrices

Matrix A :

6     5     7

9     8     6

4     3     8

Matrix B :

1     2     3

4     3     2

2     1     6

Resultant Matrix is subtraction of matrices A and B :

5      3     4

5      5     4

2      2     2

Also Read: Program for addition of two matrices

#include<iostream.h>

#include<conio.h>

int main()

{

clrscr();

int a[10][10];

int b[10][10];

int x,y,i,j;

cout<<“\nEnter the number of rows and columns :::\n\n”;

cin>>x>>y;

cout<<“\n\nEnter elements for Matrix A :::\n\n”;

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cin>>a[i][j];

}

cout<<“\n”;

}

cout<<“\n\nEnter elements for Matrix B :::\n\n”;

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cin>>b[i][j];

}

cout<<“\n”;

}

cout<<“\n\nMatrix A :\n\n”;

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cout<<“\t”<<a[i][j];

}

cout<<“\n\n”;

}

cout<<“\n\nMatrix B :\n\n”;

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cout<<“\t”<<b[i][j];

}

cout<<“\n\n”;

}

cout<<“\n\nSubtraction of Matrix A and Matrix B :\n\n”;

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cout<<“\t”<<a[i][j]-b[i][j];

}

cout<<“\n\n”;

}

getch();

return 0;

}