C++ Program to Subtract Two Matrices Using Multidimensional Arrays

In this program, the user is asked to enter the elements of the two matrices a and b. Then the elements of these two matrices are subtracted and saved it in third matrix. Finally, the result (third matrix) is printed on the screen.



Source Code
#include<iostream>
using namespace std;
int main()
{
    int a[3][3], b[3][3], c[3][3], i, j;

    cout<<"Enter the first matrix\n";
    for(i=0;i <3; i++)
    {
        for(j=0; j<3; j++)
        {
            cin>> a[i][j];
        }
    }
    cout<<"Enter the second matrix\n";
    for(i=0;i <3; i++)
    {
        for(j=0; j<3; j++)
        {
            cin>> b[i][j];
        }
    }
    cout<<"Subtraction of matrices A & B is\n";
    for(i=0;i <3; i++)
    {
        for(j=0; j<3; j++)
        {
            c[i][j] = a[i][j] - b[i][j];
            cout<< c[i][j] << " ";
        }
        cout<<"\n";
    }
}
Output
Enter the first matrix
4 5 6
7 8 9
1 2 3
Enter the second matrix
8 4 3
5 7 9
12 4 9
Subtraction of matrices A & B is
-4 1 3
2 1 0
-11 -2 -6





"Coding Hub - Learn to code" app now available on Google Play Store