C++ Program to multiply two matrices
In mathematics, to multiply two matrix what we do. First we check the number of column in first matrix is equal to number of rows in second matrix. If they are not equal then we can't multiply them.
/* C++ Program to multiply two matrices */ #include<iostream> using namespace std; int main() { int m1[3][3], m2[3][3], m3[3][3] ,i, j,k; cout<<"Enter first matrix elements: "<<endl; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>m1[i][j]; } } cout<<"Enter second matrix elements: "<<endl; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cin>>m2[i][j]; } } /* Multiply m1 and m2 matrix and store data in m3 */ for(i=0;i<3;i++) { for(j=0;j<3;j++) { // initialize element of m3 matrix to 0 and then add //multiplied elements of m1 and m2 into this m3[i][j]=0; for(k=0;k<3;k++) { m3[i][j]=m3[i][j]+m1[i][k]*m2[k][j]; } } } /* Print m3 matrix */ cout<<"Multiplication of m1 and m2"<<endl; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<m3[i][j]<<" "; } cout<<endl; } return 0; }