Java Program to Multiply Two Matrices

  • Java program to multiply two matrices using for loops.
  • Algorithm for matrix multiplication.

In this java program, we have to multiply two matrices and store the result in another product matric. It first asks user to enter the dimensions of first matrix and then populates the elements of first matrix by taking input from user. Then it asks user to enter the dimension of second matrix. The columns in first matrix must be equal to rows of second matrix. To multiply both matrices, it uses the algorithm mentioned here matrix multiplication algorithm.

Points to Remember about Matrix Multiplication
  • Two matrices A(M X N) and B(P X Q) can be multiplied if and only if N is equal to P.
  • The product of two matrices A(M X N) and B(N X Q), denoted by A x B, is a matrix of dimension M × Q.
  • The time complexity of matrix multiplication is O(n3).
For Example,
Let A be a N x M matrix and B be a M x P matrix. Then AB will be a N x P matrix.

Java program to perform matrix multiplication

package com.tcc.java.programs;

import java.util.Scanner;

public class MatrixMultiplication {

    public static void main(String[] args) {
        int i, j, k, rowF, rowS, colF, colS;
        int first[][] = new int[10][10];
        int second[][] = new int[10][10];
        int product[][] = new int[10][10];

        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter Rows and Cols of First Matrix");
        rowF = scanner.nextInt();
        colF = scanner.nextInt();

        System.out.println("Enter Elements of First Matrix");

        // Input first matrix from user
        for (i = 0; i < rowF; i++) {
            for (j = 0; j < colF; j++) {
                first[i][j] = scanner.nextInt();
            }
        }

        System.out.println("Enter Rows and Cols of Second Matrix");
        rowS = scanner.nextInt();
        colS = scanner.nextInt();

        System.out.println("Enter Elements of Second Matrix");

        // Input second matrix from user
        for (i = 0; i < rowS; i++) {
            for (j = 0; j < colS; j++) {
                second[i][j] = scanner.nextInt();
            }
        }

        // Multiplying two matrices
        for (i = 0; i < rowF; i++) {
            for (j = 0; j < colF; j++) {
                for (k = 0; k < colS; k++) {
                    product[i][j] += first[i][k] * second[k][j];
                }
            }
        }

        // Printing Product Matrix
        System.out.println("Product Matrix");
        for (i = 0; i < rowF; i++) {
            for (j = 0; j < colS; j++) {
                System.out.print(product[i][j] + " ");
            }
            System.out.print("\n");
        }
    }
}
Output
Enter Rows and Cols of First Matrix
3 3
Enter Elements of First Matrix
1 2 3
4 5 6
7 8 9
Enter Rows and Cols of Second Matrix
3 3
Enter Elements of Second Matrix
1 0 1
0 1 1
1 1 1
Product Matrix
4 5 6 
10 11 15 
16 17 24  
Enter Rows and Cols of First Matrix
3 3
Enter Elements of First Matrix
1 2 3
4 5 6
7 8 9
Enter Rows and Cols of Second Matrix
3 3
Enter Elements of Second Matrix
1 0 0
0 1 0
0 0 1
Product Matrix
1 2 3 
4 5 6 
7 8 9