# C++ Program to Subtract Two Matrices

In this article, you will learn and get code to subtract two matrices using a C++ program. Here are the list of programs on matrix subtraction:

• Subtract Two 3*3 Matrices
• Subtract and Print A-B=C. A is first and B is second matrix
• Allow user to Define the Size for Matrix

But before going through these programs, if you're not aware about the topic, then refer to matrix subtraction article to get every required things.

Note - In matrix subtraction, same row and column element of second matrix gets subtracted from same row and column element of first matrix. Let me shows you the example:

```1  2  3     0  1  3     1  1  0
4  5  6  -  3  1  2  =  1  4  4
7  8  9     1  2  3     6  6  6```

As you can see, the element at first row first column of second matrix (0) gets subtracted from element at first row first column of first matrix (1). Similarly element at first row second column of second matrix (1) gets subtracted from element at first row second column of first matrix (2), and so on.

## Subtract Two 3*3 Matrices

To subtract two matrices in C++ programming, you have to ask from user to enter the two 3*3 matrices. That is 9 elements (numbers) for first and 9 elements for second matrix. Then subtract the second matrix from first as shown in the program given below:

```#include<iostream>
using namespace std;
int main()
{
int matOne, matTwo, matSub, i, j;
cout<<"Enter 9 Elements for First Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matOne[i][j];
}
cout<<"Enter 9 Elements for Second Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matTwo[i][j];
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
matSub[i][j] = matOne[i][j] - matTwo[i][j];
}
cout<<"\nThe New Matrix (Subtraction Result) is:\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<matSub[i][j]<<"  ";
cout<<endl;
}
cout<<endl;
return 0;
}```

This program was build and run under Code::Blocks IDE. Here is its sample run:

Now supply inputs say 9, 8, 7, 6, 5, 4, 3, 2, 1 as 9 elements for first matrix, and 8, 7, 6, 5, 4, 3, 2, 1, 0 as 9 elements for second matrix. Here is the sample output after subtracting two given matrices:

When user enters 9, 8, 7, 6, 5, 4, 3, 2, 1 as 9 elements for first matrix, then it gets stored in matOne[][] in this way:

• matOne=9
• matOne=8
• matOne=7
• matOne=6
• matOne=5
• and so on upto
• matOne=1

Whereas the 9 elements say 8, 7, 6, 5, 4, 3, 2, 1, 0 gets stored in second matrix say matTwo[][] in similar way, that is:

• matTwo=8
• matTwo=7
• matTwo=6
• and so on upto
• matTwo=0

Now the matrix subtraction with following block of code gets performed:

```for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
matSub[i][j] = matOne[i][j] - matTwo[i][j];
}```

When we evaluate this block of code, then its dry run goes like:

• matSub = matOne - matTwo
• matSub = matOne - matTwo
• matSub = matOne - matTwo
• matSub = matOne - matTwo
• matSub = matOne - matTwo
• matSub = matOne - matTwo
• matSub = matOne - matTwo
• matSub = matOne - matTwo
• matSub = matOne - matTwo

Now on putting the number or values for each:

• matSub = 9 - 8
• matSub = 8 - 7
• matSub = 7 - 6
• matSub = 6 - 5
• matSub = 5 - 4
• matSub = 4 - 3
• matSub = 3 - 2
• matSub = 2 - 1
• matSub = 1 - 0

As you can see that the subtraction result is 1 in all cases, therefore in every indexes of matSub[][], 1 gets stored.

#### Subtract Two Matrices and Print A-B=C

This program is similar to previous program. But there is an extra feature included in this program. That extra feature is, this program shows the generalized way of matrix subtraction with A-B=C. Here A is first matrix, B is second matrix, and C is the subtraction result of both matrices.

```#include<iostream>
using namespace std;
int main()
{
int matOne, matTwo, matSub, i, j;
cout<<"Enter 9 Elements for First Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matOne[i][j];
}
cout<<"Enter 9 Elements for Second Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matTwo[i][j];
}
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
matSub[i][j] = matOne[i][j] - matTwo[i][j];
}
cout<<"\nSubtraction of Two Matrix is:\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<matOne[i][j]<<"  ";
if(i==1)
cout<<"  -\t";
else
cout<<"\t";
for(j=0; j<3; j++)
cout<<matTwo[i][j]<<"  ";
if(i==1)
cout<<"  =\t";
else
cout<<"\t";
for(j=0; j<3; j++)
cout<<matSub[i][j]<<"  ";
cout<<endl;
}
cout<<endl;
return 0;
}```

Here is its sample run with user input 1, 2, 3, 4, 5, 6, 7, 8, 9 as first matrix elements, and 0, 1, 2, 1, 2, 3, 1, 5, 3 as second matrix elements:

### Subtract Two Matrices of Given Size

This program allows user to define the size for matrix before entering the elements. Rest of the things are similar to very first program of this article:

```#include<iostream>
using namespace std;
int main()
{
int rowOne, colOne, rowTwo, colTwo, i, j;
int matOne, matTwo, matSub;
cout<<"Enter Row and Column Size for First Matrix: ";
cin>>rowOne>>colOne;
cout<<"Enter Row and Column Size for Second Matrix: ";
cin>>rowTwo>>colTwo;
if(rowOne==rowTwo && colOne==colTwo)
{
cout<<"\nEnter "<<rowOne*colOne<<" Elements for First Matrix: ";
for(i=0; i<rowOne; i++)
{
for(j=0; j<colOne; j++)
cin>>matOne[i][j];
}
cout<<"\nEnter "<<rowTwo*colTwo<<" Elements for Second Matrix: ";
for(i=0; i<rowTwo; i++)
{
for(j=0; j<colTwo; j++)
cin>>matTwo[i][j];
}
cout<<"\nThe New Matrix (Subtraction Result) is:\n";
for(i=0; i<rowOne; i++)
{
for(j=0; j<colOne; j++)
{
matSub[i][j] = matOne[i][j] - matTwo[i][j];
cout<<matSub[i][j]<<"  ";
}
cout<<endl;
}
}
else
cout<<"\nRow and Column Size of Both Matrix Mismatched!";
cout<<endl;
return 0;
}```

Now supply the following inputs:

• 2 as row and 3 as column size for first matrix
• 2 as row and 3 as column size for second matrix
• 1, 2, 3, 4, 5, 6 as 6 elements for first matrix
• 0, 1, 1, 4, 2, 3 as 6 elements for second matrix

After supplying these inputs, here is the sample output:

#### Same Program in Other Languages

C++ Online Test

« Previous Program Next Program »

Like/Share Us on Facebook 😋