# C++ Program to Multiply Two Matrices

« Previous Program Next Program »

In this article, you will learn and get code on matrix multiplication in C++. Here are the list of program on matrix multiplication, available in this article:

• Multiply Two 3*3 Matrices. 3*3 Matrix means, matrix with 3 rows and 3 columns
• Multiply Two Matrices of Given Size (Dimension)

Before going through these programs, if you're not aware about the steps used to multiply two matrices, refer to matrix multiplication steps to get every required things about matrix multiplication.

## Matrix Multiplication in C++

To multiply two matrices in C++ programming, you have to ask from user to enter elements for both (first and second) matrix. Now apply the formula to multiply two matrices and initialize the multiplication result's element to the third matrix one by one as shown in the program given below:

This program doesn't allows user to enter the size of matrix, rather it asks to enter 9 elements for first and 9 elements for second 3*3 matrix. Later on, the same program that allows to enter the size of matrix too, is given.

The question is, write a program in C++ to multiply two matrices. Here is its answer:

```#include<iostream>
using namespace std;
int main()
{
int matOne, matTwo, matThree;
int i, j, k, sum=0;
cout<<"Enter 9 Elements for First Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matOne[i][j];
}
cout<<"\nEnter 9 Elements for Second Matrix: ";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cin>>matTwo[i][j];
}
// Multiplying two matrices...
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
sum=0;
for(k=0; k<3; k++)
sum = sum + (matOne[i][k] * matTwo[k][j]);
matThree[i][j] = sum;
}
}
cout<<"\nMultiplication Result:\n";
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
cout<<matThree[i][j]<<"\t";
cout<<endl;
}
cout<<endl;
return 0;
}```

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

Now supply or enter 9 numbers as 9 elements for first and again 9 numbers as 9 elements for second matrix. And then press `ENTER` key to multiply both the matrices and print the third matrix (stores multiplication result of two given matrix) as shown in the snapshot given below:

### Multiply Two Matrices of Given Size

This program multiply two matrices of given size by user at run-time. That is, in addition of asking to enter elements for both the matrix, this program first receives the size and then ask to enter elements of given size for both first and second matrix.

```#include<iostream>
using namespace std;
int main()
{
int matOne, matTwo, matThree;
int matOneRow, matOneCol, matTwoRow, matTwoCol;
int i, j, k, sum;
cout<<"Enter the Row Size of First Matrix: ";
cin>>matOneRow;
cout<<"Enter the Column Size of First Matrix: ";
cin>>matOneCol;
cout<<"\nEnter "<<matOneRow*matOneCol<<" Elements for First Matrix: ";
for(i=0; i<matOneRow; i++)
{
for(j=0; j<matOneCol; j++)
cin>>matOne[i][j];
}
cout<<"\nEnter the Row Size of Second Matrix: ";
cin>>matTwoRow;
cout<<"Enter the Column Size of Second Matrix: ";
cin>>matTwoCol;
cout<<"\nEnter "<<matTwoRow*matTwoCol<<" Elements for Second Matrix: ";
for(i=0; i<matTwoRow; i++)
{
for(j=0; j<matTwoCol; j++)
cin>>matTwo[i][j];
}
if(matOneCol != matTwoRow)
{
cout<<"\nMultiplication Not Possible!\n";
return 0;
}
// Multiplying the two matrix...
for(i=0; i<matOneRow; i++)
{
for(j=0; j<matTwoCol; j++)
{
sum = 0;
for(k=0; k<matOneCol; k++)
sum = sum + (matOne[i][k] * matTwo[k][j]);
matThree[i][j] = sum;
}
}
cout<<"\nMultiplication Result:\n";
for(i=0; i<matOneRow; i++)
{
for(j=0; j<matTwoCol; j++)
cout<<matThree[i][j]<<"\t";
cout<<endl;
}
cout<<endl;
return 0;
}```

Here is its sample run with following user inputs:

• 2 as size of row for first matrix
• 3 as size of column for first matrix
• 1, 2, 3, 4, 5, 6 as six elements for first matrix
• 3 as size of row for second matrix
• 2 as size of column for second matrix
• 7, 8, 9, 10, 11, 12 as six elements for second matrix

After providing these inputs, press `ENTER` key to multiply two given matrices and print the multiplication result as shown in the sample output given below:

Note - To define matrix multiplication, number of columns in first matrix must be equal to the number of rows in second matrix.

Therefore, before starting the multiplication, we've checked whether the value of matOneCol is not equal to the value of matTwoRow or not. If condition satisfies, then matrix multiplication can't be defined, otherwise define the matrix multiplication and print the result as shown in the program given above.

#### Same Program in Other Languages

C++ Online Test

« Previous Program Next Program »