## Introduction

In this article, we will understand the matrix multiplication with the example and find the multiplication of two matrices in Java and C++. Matrix multiplication is a binary operation that produces a matrix from two matrices. For matrix multiplication, the range of columns inside the first matrix must be identical to the wide variety of rows in the 2d matrix. The resulting matrix is â€‹â€‹called a matrix product and has a variety of rows in the first matrix and a range of columns in the second matrix.

Matrix multiplication

Recommended Topic: __AngularJS__

### Problem statement

We are given two matrices with sizes of rows and columns. The task is to multiply both the matrices and print the resultant matrix.

### Examples

**Input**

`a[2][2] = { {1, 3}, {2, 4} }`

` b[2][2] = { {3, 1} {4, 2} }`

**Output**

` result[2][2] = { {15, 7}, {22, 10} }`

**Input**

` a[3][2] = { {1, 3}, {2, 4},{5,6} }`

` b[2][3] = { {3, 1,2} , {4,7,8} }`

**Output**

` result[3][3] = { {15,22,26}, {22,30,36},{39,47,58} } `

## Approach

For solving this problem, we will first input the two matrices from the user. Then we will check if the matrix can be multiplied or not by checking that the column of the first matrix should be equal to the row of the second matrix. If that condition satisfies then only the matrix will be multiplied. After that we will simply pass the 3 loops to multiply the two matrices by the formula c[i][j]+=a[i][k]*b[k][j]. In the end, we will print the resultant matrix.

### Pseudocode

```
for(int i=0;i<row1;i++){
for(int j=0;j<col2;j++){
c[i][j]=0;
for(int k=0;k<col1;k++){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
```

### Java code

```
// java program to find the matrix multiplication.
import java.util.*;
class matrixmuli
{
public
static void main(String args[])
{
int[][] a = new int[10][10];
int[][] b = new int[10][10];
int[][] c = new int[10][10];
int row1, col1, row2, col2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the rows of first matrix");
row1 = sc.nextInt();
System.out.println("Enter the col of first matrix");
col1 = sc.nextInt();
System.out.println("Enter the rows of second matrix");
row2 = sc.nextInt();
System.out.println("Enter the col of second matrix");
col2 = sc.nextInt();
if (col1 != row2)
{
System.out.println(" -----please enter valid inputs----");
// break;
}
else
{
// taking the elements of first matrix by user
System.out.println("Enter the elements of first matrix ");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
a[i][j] = sc.nextInt();
}
}
// taking the elements of first matrix by user
System.out.println("Enter the elements of second matrix ");
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
b[i][j] = sc.nextInt();
}
}
// matrix multiplication code
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
// making third matrix element to 0 for avoiding garbage values
c[i][j] = 0;
for (int k = 0; k < col1; k++)
{
// code to multiply two matrices
c[i][j] += a[i][k] * b[k][j];
}
}
}
// printing the multiplied matrix
System.out.println("\nMatrix after multiplication");
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println("\n");
}
}
}
}
```

**Output**

### C++ code

```
// C++ program to find the matrix multiplication.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int row1, col1, row2, col2;
cout << "Enter the number of rows of first matrix ";
cin >> row1;
cout << "Enter the number of column of first matrix ";
cin >> col1;
cout << "Enter the number of rows of second matrix ";
cin >> row2;
cout << "Enter the number of column of second matrix ";
cin >> col2;
if (col1 != row2)
{
cout << " -----please enter valid inputs----";
}
else
{
int a[row1][col1], b[row2][col2], c[row1][col2];
cout << "Enter the elements of first matrix\n";
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
{
cin >> a[i][j];
}
}
cout << "Enter the elements of second matrix\n";
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
cin >> b[i][j];
}
}
// matrix multiplication code
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
// making third matrix element to 0 for avoiding garbage values
c[i][j] = 0;
for (int k = 0; k < col1; k++)
{
// code to multiply two matrices
c[i][j] += a[i][k] * b[k][j];
}
}
}
cout << "\nMatrix after multiplication" << endl;
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
{
cout << c[i][j] << " ";
}
cout << endl;
}
}
}
```

**Output**

#### Complexity analysis

**Time complexity**

Since we are using three loops to multiply the elements. Therefore the time complexity would be O(n1*m1*m2), where n1 is the size of the row of the first matrix and m1 is the size of the column of the first matrix and m2 is the size of the column of the second matrix. You can consider it approximately equal to O(n^3).

**Space complexity**

Since we are using a new matrix of the size of n3 and m3 to store the elements therefore the space complexity would be O(n3*m3), n is the size of the row of the new matrix and m is the size of the column of the new matrix. You can consider it approximately equal to O(n*m).

Also see, __Rabin Karp Algorithm__