# Java Program to Multiply Two Matrices

«
»

Matrix multiplication in Java is the process of multiplying two matrices using the dot product of rows and columns, calculated using nested for loops. The resulting matrix has the same number of rows as the first matrix and the same number of columns as the second matrix.

Constraint: For Multiplication of two matrices, the columns of the first matrix must be equal to the rows of the second matrix.

Suppose the first matrix is of the order of [m ,n], so the second matrix should be of the order of [n ,p], else the two matrices can’t be multiplied. The new matrix produced in the above case would be of the order of [m,p].

Examples:

If the first matrix is of order [1,3] and second is of order [3,5], the two matrices can be multiplied together and the new matrix will be of order [1,5]. However, if the first matrix is of order [1,3] and the second is of order [1,5], they cannot be multiplied together.

Example 1:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$$\begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix}$$ * $$\begin{bmatrix} 2 & 3 & 4\\ 5 & 6 & 7\\ 8 & 9 & 1 \end{bmatrix}$$ = $$\begin{bmatrix} 1*2 + 2*5 + 3*8 & 1*3 + 2*6 + 3*9 & 1*4 + 2*7 + 3*1\\ 4*2 + 5*5 + 6*8 & 4*3 + 5*6 + 6*9 & 4*4 + 5*7 + 6*1\\ 7*2 + 8*5 + 9*8 & 7*3 + 8*6 + 9*9 & 7*4 + 8*7 + 9*1 \end{bmatrix}$$ = $$\begin{bmatrix} 36 & 42 & 21\\ 81 & 96 & 57\\ 126 & 150 & 93 \end{bmatrix}$$

Example 2:

$$\begin{bmatrix} 1 & 4 & 2\\ 3 & 3 & 2 \end{bmatrix}$$ * $$\begin{bmatrix} 1\\ 2\\ 3 \end{bmatrix}$$ = $$\begin{bmatrix} 1*1 + 4*2 + 2*3\\ 3*1 + 3*2 + 2*3 \end{bmatrix}$$ = $$\begin{bmatrix} 15\\ 15 \end{bmatrix}$$

Check this: BCA MCQs | Programming Books
Problem Description

Write a Java program for multiplication of two matrices.

Program/Source Code

Here is the source code of the Java Program to Perform Matrix Multiplication. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

// This is sample program for matrix multiplication
// The complexity of the algorithm is O(n^3)

package com.sanfoundry.numerical;

import java.util.Scanner;

public class MatixMultiplication
{
public static void main(String args[])
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the base of squared matrices");
n = input.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
int[][] c = new int[n][n];
System.out.println("Enter the elements of 1st martix row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = input.nextInt();
}
}
System.out.println("Enter the elements of 2nd martix row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = input.nextInt();
}
}
System.out.println("Multiplying the matrices...");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("The product is:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
input.close();
}
}
Program Explanation

1. This Java program takes input two square matrices and outputs their product.
2. The program first prompts the user to enter the size of the matrices.
3. It then prompts the user to enter the elements of the two matrices row by row.
4. The program uses nested for loops to read the input and store it in two 2D arrays “a” and “b”.
5. The program then multiplies the two matrices using another nested for loop and the standard matrix multiplication algorithm.
6. The product is stored in a 2D array “c”.
7. Finally, the program outputs the product matrix to the console row by row and closes the input stream.

Time Complexity: O(n3)
The time complexity of the matrix multiplication algorithm in Java is O(n3), where n is the size of the square matrices.

Space Complexity: O(n2)
The space complexity of the Java program for matrix multiplication is O(n2), as it declares three 2D arrays of size n x n to store the input matrices and their product, where n is the size of the square matrices.

Runtime Testcases

In this case, the columns of the first matrix must be equal to the rows of the second matrix.

$javac MatixMultiplication.java$ java MatixMultiplication

Enter the base of squared matrices:
3
Enter the elements of 1st martix row wise:
1 2 3
4 5 6
7 8 9
Enter the elements of 2nd martix row wise:
2 3 4
5 6 7
8 9 1
Multiplying the matrices...
The product is:
36 42 21
81 96 57
126 150 93

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.