Multiply matrices java – Java Program to Multiply Two Matrices

Multiply matrices java: In the previous article, we have discussed Java Program to Subtract Two Matrices

In this article we are going to see how we can write a program to calculate the product of two matrices in JAVA language.

Java Program to Multiply Two Matrices

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Let’s see different ways to multiply two matrices.

Method-1: Java Program to Multiply Two Matrices By Static Initialization of Array Elements

Approach:

  • Initialize and declare two arrays of size 3×3 with elements.
  • Use two for loops to iterate the rows and columns .
  • Inside the for loops set all the elements of the resultant matrix to 0, and there create another loop to multiply the elements and store them.
  • Print the resultant array.

Program:

import java.util.Scanner;
public class Matrix{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        int mat1[][] = {{19,25,32},{40,54,62},{70,20,60}};
        int mat2[][] =  {{50,15,10},{98,45,78},{23,73,50}};
        int res[][] = new int[3][3];
        int row, col ;
        // Printing the matrices using our user-defined function
        System.out.print("\nMatrix 1 : ");
        printMatrix(mat1);
        System.out.print("\nMatrix 2 : ");
        printMatrix(mat2);

        // Loops to find product of two matrices
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {   
                // Initializes the array element to zero first
                res[row][col] = 0;
                for(int i = 0;i<3;i++)
                    res[row][col]=mat1[row][i]*mat2[i][col];
            }
        System.out.print("\nAfter multiplication of both the matices : ");
        printMatrix(res);
    }

    // Function to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }
}

Output:

Matrix 1 : 
19 25 32 
40 54 62 
70 20 60 

Matrix 2 : 
50 15 10 
98 45 78 
23 73 50 

After multiplication of both the matices : 
736   2336 1600 
1426 4526 3100 
1380 4380 3000

Method-2: Java Program to Multiply Two Matrices By Dynamic Initialization of Array Elements

Approach:

  • Initialize and declare two arrays of size 3×3.
  • Ask the user for input and store them in the arrays using two for loops.
  • Use two for loops to iterate the rows and columns.
  • Inside the for loops set all the elements of the resultant matrix to 0, and there create another loop to multiply the elements and store them.
  • Print the resultant array.

Program:

import java.util.Scanner;
public class Matrix{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        int mat1[][] = new int[3][3];
        int mat2[][] =  new int[3][3];
        int res[][] = new int[3][3];
        int row, col ;
        
        // Taking matrix1 input
        System.out.print("\nEnter the first matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                mat1[row][col] = scan.nextInt();
            
        System.out.print("\nMatrix 1 :");
        //calling the printMatrix() method to print matrix 1    
        printMatrix(mat1);
        
        // Taking matrix2 input
        System.out.print("\nEnter the second matrix elements : ");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                mat2[row][col] = scan.nextInt();
        
         System.out.print("\nMatrix 2 :");
        //calling the printMatrix() method to print matrix 1    
        printMatrix(mat2);

        // Loops to find product of two matrices
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
            {   
                // Initializes the array element to zero first
                res[row][col] = 0;
                for(int i = 0;i<3;i++)
                    res[row][col]+=mat1[row][i]*mat2[i][col];
            }
        System.out.print("\nAfter multiplication of both the matrices : ");
        printMatrix(res);
    }

    // Function to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }
}


Output:

Enter the first matrix elements : 1 2 3 4 5 6 7 8 9
Matrix 1 :
1 2 3 
4 5 6 
7 8 9

Enter the second matrix elements : 1 2 3 4 5 6 7 8 9
Matrix 2 :
1 2 3 
4 5 6 
7 8 9

After multiplication of both the matrices : 
30 36 42 
66 81 96 
102 126 150

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: