Java With Us
Home   |   Tutorial   |   Programs

Matrix Multiplication - Java Program

This article shows you how to write a matrix multiplication program in Java. A three level nested loop is used to perform the multiplication.

Let the two matrix to be multiplied be A and B. Let A be a matrix of order d*e - d rows and e columns and B be the second matrix of order e*f. Note that the number of columns in the first matrix should be the same as the number of rows in the second matrix. The product matrix will be of order d*f. Now, our task is to find what numbers go into each of the positions of matrix C with d rows and f columns. We use an outer loop with loop counter i ranging from 0 to d. An inner loop has the loop counter j ranging from 0 to f. At any iteration of the loop, i refers to the row number and j to the column number of the position in matrix C that we are trying to fill. Now, within the body of the inner loop, we have to write some code which will calculate the value to be filled. This body will in turn consist of a loop. When we are obtaining the value at C[i][j], row i of matrix A and column j of matrix B are multiplied element wise.

C[i][j] = A[i][0] * B[0][j] + A[i][1] * B[1][j] + A[i][2] * B[2][j] + .... A[i][e-1] * B[e-1][j]

Both row i and column j have e elements. An loop is used whose counter k, ranges from 0 to e-1. Within the loop, A[i][k] and B[k][j] are multiplied and the product obtained is added to the existing value of C[i][j]. We can also declare a variable sum before the start of the innermost loop, add the element wise products to this variable and assign the resulting sum to C[i][j].

The complete Java program for matrix multiplication is given below.

import java.util.Scanner;

public class MatrixMultiplication {

   public static void main(String[] args) {
       Scanner s = new Scanner(;
       System.out.print("Enter number of rows in A: ");
       int rowsInA = s.nextInt();
       System.out.print("Enter number of columns in A / rows in B: ");
       int columnsInA = s.nextInt();
       System.out.print("Enter number of columns in B: ");
       int columnsInB = s.nextInt();
       int[][] a = new int[rowsInA][columnsInA];
       int[][] b = new int[columnsInA][columnsInB];
       System.out.println("Enter matrix A");
       for (int i = 0; i < a.length; i++) {
           for (int j = 0; j < a[0].length; j++) {
               a[i][j] = s.nextInt();
       System.out.println("Enter matrix B");
       for (int i = 0; i < b.length; i++) {
           for (int j = 0; j < b[0].length; j++) {
               b[i][j] = s.nextInt();
       int[][] c = multiply(a, b);
       System.out.println("Product of A and B is");
       for (int i = 0; i < c.length; i++) {
           for (int j = 0; j < c[0].length; j++) {
               System.out.print(c[i][j] + " ");

   public static int[][] multiply(int[][] a, int[][] b) {
       int rowsInA = a.length;
       int columnsInA = a[0].length; // same as rows in B
       int columnsInB = b[0].length;
       int[][] c = new int[rowsInA][columnsInB];
       for (int i = 0; i < rowsInA; i++) {
           for (int j = 0; j < columnsInB; j++) {
               for (int k = 0; k < columnsInA; k++) {
                   c[i][j] = c[i][j] + a[i][k] * b[k][j];
       return c;

Here is a sample execution of the above program.

Enter number of rows in A: 2
Enter number of columns in A / rows in B: 3
Enter number of columns in B: 3
Enter matrix A
1 3 4
2 5 6
Enter matrix B
1 5 6
2 5 4
1 1 1
Product of A and B is
11 24 22
18 41 38

Privacy Policy