Java Program to multiply two matrices

Program

import java.util.Scanner;
public class MatrixMultiplication {
    public static void main(String[] args)
    {
    int mat1[][] = new int[10][10];
    int mat2[][] = new int[10][10];    
    int result[][] = new int[10][10];
	int row1, col1;
	int row2, col2;
	int i, j, k;
    Scanner reader = new Scanner(System.in);
	System.out.println("Enter the number of rows and columns for matrix 1:");
	System.out.println("Row 1:\t");	
    row1 = reader.nextInt();
	System.out.println("Column 1:\t");
	col1 = reader.nextInt();
	System.out.println("Enter the number of rows and columns for matrix 2:");
	System.out.println("Row 2:\t");
	row2 = reader.nextInt();
	System.out.println("Column 2:\t");
	col2 = reader.nextInt();
	if (col1 != row2)
	{
		System.out.println("Unable to perform matrix multiplication");
	}
	else
	{
		System.out.println("Enter elements for martix 1:");
		for(i = 0; i < row1; i++)
		{
			for(j = 0; j < col1; j++)
			{
				mat1[i][j] = reader.nextInt();
			}
		}
		System.out.println("Enter elements for martix 2:");
		for(i = 0; i < row2; i++)
		{
			for(j = 0; j < col2; j++)
			{
				mat2[i][j] = reader.nextInt();
			}
		}
		for(i = 0; i < row1; i++)
		{
			for(j = 0; j < col2; j++)
			{
				result[i][j] = 0;
				for(k = 0; k < col1; k++)
				{
					result[i][j] += mat1[i][k] * mat2[k][j];
				}
			}
		}
		System.out.println("Product matrix:");
		for(i = 0; i < row1; i++)
		{
			for(j = 0; j < col2; j++)
			{
				System.out.printf("%d\t", result[i][j]);
			}
			System.out.println("\n");
		}
	}
    }
}

This program multiplies two matrices and prints the resultant matrix. Below is a step-by-step explanation:

1. Matrix Initialization

  • Three 2D arrays (mat1, mat2, result) are initialized to store:
    • First matrix (mat1)
    • Second matrix (mat2)
    • Resultant matrix (result)

2. User Input for Matrix Dimensions

  • The user is prompted to enter the number of rows and columns for both matrices.
  • The matrices must satisfy matrix multiplication conditions:
    • Condition: The number of columns in mat1 must be equal to the number of rows in mat2 (i.e., col1 == row2).
    • If this condition is not met, multiplication is not possible, and the program displays "Unable to perform matrix multiplication".

3. Input Elements for Matrices

  • The program takes user input for both matrices by iterating through the respective row-column indices.

4. Matrix Multiplication Logic

The program follows the standard matrix multiplication rule:

[ C[i][j] = \sum_{k=0}^{col1-1} A[i][k] \times B[k][j] ]

  • The result[i][j] is initialized to 0 before calculating the sum of products.
  • A nested loop structure is used:
    1. Outer loop (i) iterates over rows of mat1.
    2. Middle loop (j) iterates over columns of mat2.
    3. Inner loop (k) computes the dot product of row i from mat1 and column j from mat2.

5. Displaying the Resultant Matrix

  • The final product matrix is displayed in a tabular format using System.out.printf().

Key Points

  • Handles invalid matrix dimensions
  • Uses standard matrix multiplication algorithm
  • Efficient use of nested loops
  • Displays formatted output

Example Input/Output

Input

Enter the number of rows and columns for matrix 1:
Row 1: 2
Column 1: 3
Enter the number of rows and columns for matrix 2:
Row 2: 3
Column 2: 2

Enter elements for matrix 1:
1 2 3
4 5 6

Enter elements for matrix 2:
7 8
9 10
11 12

Matrix Multiplication

Product matrix:
  58   64
 139  154

Output 1

$ javac MatrixMultiplication.java
$ java MatrixMultiplication
Enter the number of rows and columns for matrix 1:
Row 1:
2
Column 1:
3
Enter the number of rows and columns for matrix 2:
Row 2:
3
Column 2:
3
Enter elements for martix 1:
2
3
1
2
-7
4
Enter elements for martix 2:
3
4
5
1
1
4
2
1
4
Product matrix:
11      12      26
7       5       -2

Output 2

$ java MatrixMultiplication
Enter the number of rows and columns for matrix 1:
Row 1:
2
Column 1:
2
Enter the number of rows and columns for matrix 2:
Row 2:
3
Column 2:
2
Unable to perform matrix multiplication