Multiply Two Matrices in C++

Last Updated : 21 Aug, 2026

Matrix multiplication produces a new matrix by multiplying the rows of the first matrix with the columns of the second matrix. Two matrices can be multiplied only when the number of columns in the first matrix equals the number of rows in the second matrix.

  • If the first matrix is of size (R_1 \times C_1) and the second is (R_2 \times C_2), the result is of size (R_1 \times C_2).
  • Each element of the result is calculated as the sum of products of corresponding elements.

Example

Input:
mat1[][] = {{1, 2},
{3, 4}}
mat2[][] = {{5, 6},
{7, 8}}

Multiplication of two matrices:

{{1*5 + 2*7 1*6 + 2*8},
{3*5 + 4*7 3*6 + 4*8}}
Output:
{{19, 22},
{43, 50}}

Image
Matrix Multiplication in C++

Approach

The idea is to use three nested loops to calculate each element of the resultant matrix.

  • Check whether the number of columns in the first matrix equals the number of rows in the second matrix.
  • Initialize a resultant matrix of size (R_1 \times C_2).
  • Use the first loop to traverse the rows of the first matrix.
  • Use the second loop to traverse the columns of the second matrix.
  • Use the third loop to multiply corresponding elements and add them to the result.
  • Repeat this for every element of the resultant matrix.
C++
#include <iostream>
using namespace std;

const int MAX = 100;

// Function to multiply two matrices
void multiplyMatrices(
    int mat1[][MAX],
    int mat2[][MAX],
    int result[][MAX],
    int r1, int c1,
    int r2, int c2)
{
    // Initialize result matrix
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            result[i][j] = 0;
        }
    }

    // Multiply the matrices
    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            for (int k = 0; k < c1; k++) {
                result[i][j] +=
                    mat1[i][k] * mat2[k][j];
            }
        }
    }
}

int main()
{
    int mat1[][MAX] = {
        {1, 2},
        {3, 4}
    };

    int mat2[][MAX] = {
        {5, 6},
        {7, 8}
    };

    int r1 = 2, c1 = 2;
    int r2 = 2, c2 = 2;

    // Matrix multiplication is possible only if
    // columns of mat1 = rows of mat2
    if (c1 != r2) {
        cout << "Matrix multiplication is not possible";
        return 0;
    }

    int result[MAX][MAX];

    multiplyMatrices(
        mat1, mat2, result,
        r1, c1, r2, c2
    );

    cout << "Resultant Matrix:\n";

    for (int i = 0; i < r1; i++) {
        for (int j = 0; j < c2; j++) {
            cout << result[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
} 

Output
Resultant Matrix:
19 22 
43 50 

Explanation

  • multiplyMatrices() initializes the result matrix with 0.
  • The outer two loops select each position of the resultant matrix.
  • The inner loop multiplies elements from the current row of mat1 and current column of mat2.
  • The products are added to obtain result[i][j].
  • Before multiplication, the program checks whether c1 == r2, which is the required condition for matrix multiplication.
Try It Yourself
redirect icon
Comment