Java Program to Perform Matrix Multiplication

1. Introduction

Matrix multiplication is a fundamental operation in linear algebra that takes a pair of matrices and produces another matrix. In this tutorial, we will write a Java program to perform matrix multiplication.

2. Program Steps

1. Define a class named MatrixMultiplication.

2. Inside the main method, define two 2D arrays matrix1 and matrix2 representing the matrices to be multiplied.

3. Check if the number of columns in matrix1 is equal to the number of rows in matrix2. If not, matrix multiplication is not possible.

4. If multiplication is possible, define a 2D array result to store the multiplication result.

5. Use nested for loops to calculate the elements of the result matrix by summing the products of corresponding elements of matrix1 and matrix2.

6. Print the result matrix.

3. Code Program

public class MatrixMultiplication { // Step 1: Define a class named MatrixMultiplication

    public static void main(String[] args) { // Main method

        // Step 2: Define two 2D arrays matrix1 and matrix2
        int[][] matrix1 = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        int[][] matrix2 = {
            {9, 8, 7},
            {6, 5, 4},
            {3, 2, 1}
        };

        // Step 3: Check if the number of columns in matrix1 is equal to the number of rows in matrix2
        if (matrix1[0].length != matrix2.length) {
            System.out.println("Matrix multiplication is not possible.");
        } else {

            // Step 4: Define a 2D array result to store the multiplication result
            int rows = matrix1.length;
            int columns = matrix2[0].length;
            int[][] result = new int[rows][columns];

            // Step 5: Use nested for loops to calculate the elements of the result matrix
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    for (int k = 0; k < matrix2.length; k++) {
                        result[i][j] += matrix1[i][k] * matrix2[k][j];
                    }
                }
            }

            // Step 6: Print the result matrix
            System.out.println("Multiplication of the matrices:");
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < columns; j++) {
                    System.out.print(result[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
}

Output:

Multiplication of the matrices:
30 24 18
84 69 54
138 114 90

4. Step By Step Explanation

Step 1: A class named MatrixMultiplication is defined.

Step 2: Two 2D arrays matrix1 and matrix2 representing the matrices to be multiplied are defined inside the main method.

Step 3: The program checks whether the number of columns in matrix1 is equal to the number of rows in matrix2, as this condition is necessary for matrix multiplication. If not, it prints a message and exits.

Step 4: If matrix multiplication is possible, a 2D array result is defined to store the multiplication result.

Step 5: Nested for loops are used to calculate the elements of the result matrix by summing the products of corresponding elements of matrix1 and matrix2.

Step 6: The program prints the result matrix, which is the product of matrix1 and matrix2.

Comments