# C++ Programming Code Examples

## C++ > Arrays and Matrices Code Examples

### C++ Programming Code to Subtract Two Matrices

``` C++ Programming Code to Subtract Two Matrices To subtract two matrices in C++ Programming, you have to ask to the user to enter the two 3*3 matrix elements, then start subtracting the matrices i.e., subtract matrix second from matrix first like mat1[0][0]-mat2[0][0], mat1[1][1]-mat2[1][1], and so on. Store the result in the third matrix say mat3[0][0], mat3[1][1], and so on. And after subtracting the matrices, print the subtraction result on the output screen as shown here in the following program. Following C++ program ask to the user to enter any two 3*3 array elements to subtract them i.e., Matrix1 - Matrix2, then display the subtracted result of the two matrices (Matrix3) on the screen: /* C++ Program - Subtract Two Matrices */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int arr1[3][3], arr2[3][3], arr3[3][3], sub, i, j; cout<<"Enter 3*3 Array 1 Elements : "; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>arr1[i][j]; } } cout<<"Enter 3*3 Array 2 Elements : "; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cin>>arr2[i][j]; } } cout<<"Subtracting array (array1-array2) ... \n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { arr3[i][j]=arr1[i][j]-arr2[i][j]; } } cout<<"Result of Array1 - Array2 is :\n"; for(i=0; i<3; i++) { for(j=0; j<3; j++) { cout<<arr3[i][j]<<" "; } cout<<"\n"; } getch(); } ```