Java Matrix Multiplication Program with Example Input Output

Java matrix multiplication program example with input output using 2D arrays

Matrix multiplication is one of the most important programs in Java, especially for exams and practicals. It helps you understand how 2D arrays work and how nested loops are used in real programs.

In this tutorial, you will learn how to write a Java program to multiply two matrices with proper input, condition checking, and output display.


Understanding Matrix Multiplication

Before writing the code, it is important to understand the rule:

  • The number of columns in the first matrix must be equal to the number of rows in the second matrix
  • If this condition is not satisfied, multiplication is not possible

For example:

  • Matrix A = 2 × 3
  • Matrix B = 3 × 2
  • Result = 2 × 2 (valid)

Java Program for Matrix Multiplication

import java.util.Scanner;

public class MatrixMultiplication {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        System.out.println("Enter the number of rows and columns of first matrix:");
        int rowsA = scan.nextInt();
        int columnsA = scan.nextInt();

        int[][] matrixA = new int[rowsA][columnsA];

        System.out.println("Enter the elements of first matrix:");
        for (int i = 0; i < rowsA; i++) {
            for (int j = 0; j < columnsA; j++) {
                matrixA[i][j] = scan.nextInt();
            }
        }

        System.out.println("Enter the number of rows and columns of second matrix:");
        int rowsB = scan.nextInt();
        int columnsB = scan.nextInt();

        int[][] matrixB = new int[rowsB][columnsB];

        System.out.println("Enter the elements of second matrix:");
        for (int i = 0; i < rowsB; i++) {
            for (int j = 0; j < columnsB; j++) {
                matrixB[i][j] = scan.nextInt();
            }
        }

        if (columnsA != rowsB) {
            System.out.println("Matrices cannot be multiplied with the given dimensions.");
        } else {
            int[][] result = new int[rowsA][columnsB];

            for (int i = 0; i < rowsA; i++) {
                for (int j = 0; j < columnsB; j++) {
                    for (int k = 0; k < columnsA; k++) {
                        result[i][j] += matrixA[i][k] * matrixB[k][j];
                    }
                }
            }

            System.out.println("The result is:");
            for (int i = 0; i < rowsA; i++) {
                for (int j = 0; j < columnsB; j++) {
                    System.out.print(result[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
}

Explanation of the Code

  • User inputs size and elements of both matrices
  • Program checks if multiplication condition is valid
  • Uses three nested loops to perform multiplication
  • Stores result in a new matrix
  • Prints the final matrix output

The inner loop is the most important part where actual multiplication happens.


Sample Output

Example:

The result is:
58 64
139 154

Your output may vary depending on the input values.


Real-World Use Cases

  • Graphics and image processing
  • Game development calculations
  • Scientific and engineering applications
  • Machine learning and data processing

Common Mistakes

  • Forgetting to check multiplication condition
  • Using wrong loop limits
  • Confusing rows and columns
  • Not initializing result array properly

Best Practices

  • Always validate matrix dimensions before multiplication
  • Use meaningful variable names
  • Keep code properly indented
  • Test with small inputs first

Pro Tip

Matrix multiplication uses O(n³) time complexity. For large matrices, optimized algorithms are used, but for exams, this approach is perfect.


Frequently Asked Questions

Why do we need three loops?

Because each element of the result matrix is calculated using rows and columns from both matrices.

What happens if condition is not satisfied?

The program will display an error message and stop multiplication.

Can we multiply any two matrices?

No, only when columns of first matrix equal rows of second matrix.


🔗 Related Articles


Conclusion

In this guide, you learned how to implement matrix multiplication in Java using 2D arrays and nested loops. This is a very important practical program that helps build strong fundamentals in arrays and logic building.

Practice this program with different inputs and try modifying it. Don’t forget to bookmark this page and explore more Java programs from the related articles.

Comments