Subtraction of Two Matrix in C++
Here, you will get the example code of subtraction of two matrix in c++ program.
Here, first we will get size of matrix, Input elements in matrix 1 and matrix 2, calculate the subtract matrices, and print the matrix 3 as result.
Subtraction of Two Matrix in C++ program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | //Subtract matrices in CPP #include<iostream> using namespace std; main() { int x[5][5],y[5][5],z[5][5],i,j; int r,c; //input size of matrix cout<<"\nEnter No of Rows & Columns (max : 5,5) :"; cin>>r>>c; //input matrix 1 elements cout<<"\nEnter Elements for 1st Matrix : "; for(i=0;i<r;i++) { for(j=0;j<c;j++) cin>>x[i][j]; } //input matrix 2 elements cout<<"\nEnter Elements for 2nd Matrix : "; for(i=0;i<r;i++) { for(j=0;j<c;j++) cin>>y[i][j]; } // Addition of Matrix for(i=0;i<r;i++) { for(j=0;j<c;j++) z[i][j]=x[i][j]-y[i][j]; } //print matrix After substraction cout<<"\nThe Elements After Substraction : \n\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) cout<<z[i][j]<<" "; cout<<"\n"; } return 0; } |
Output
Enter No of Rows & Columns (max : 5,5) :3
3
Enter Elements for 1st Matrix :
1 2 3
6 5 4
8 7 6
Enter Elements for 2nd Matrix :
2 3 4
2 3 1
4 3 2
The Elements After Substraction :
-1 -1 -1
4 2 3
4 4 4
Other Similar Programs
Find Highest and Lowest Element of a Matrix in C++
Check out our other C programming Examples