Java program to multiply two matrices

Here, we are supposed to enter two 2D arrays or rather Matrices. The 2D-arrays will be dynamic arrays of size (R1*C1) and (R2*C2). After entering into the arrays, we’ll have to calculate and print the product matrix and along with displaying both the original matrices.

The basic idea to be kept in mind is:
Two matrices can only be multiplied by when the number of Columns of the First Matrix is equal to Number of Rows of Second Matrix.

The standard algorithm will be:

  1. Enter the size of the 2D-arrays.
  2. Check if the number of Columns of First Matrix is equal to Number of Rows of Second Matrix or not. If they aren’t equal, further operation should be immediately terminated and program execution is stopped.
  3. Otherwise, create the matrices of the input sizes.
  4. Using a loop construct, enter integer inputs in the matrices.
  5. Declare another matrix that will store the product of original matrices. The size will be (R1*C2).
  6. Using loop construct, multiply both the original matrices and store the result in product matrix.
  7. After the loop terminates, print the result accordingly.

Source Code:

/* Program to enter two 2D arrays of size m*n and p*q respectively and print the multiplication product of both the matrix */

import java.util.*; class ProductMatrix { public static void main() { Scanner inp=new Scanner(System.in); int r1,c1,r2,c2,i,j,k;

System.out.print("\n Enter Dimensions of First Matrix (Row * Column) : \n"); r1=inp.nextInt(); c1=inp.nextInt(); System.out.print("\n Enter Dimensions of Second Matrix (Row * Column) : \n"); r2=inp.nextInt(); c2=inp.nextInt();

if(c1!=r2) { System.out.println("Number of Columns of First Matrix should be equal to Number of Rows of Second Matrix"); System.exit(0); }

int a[][]=new int[r1][c1]; System.out.println("Enter into First Matrix: "); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { System.out.print("\n Enter: "); a[i][j]=inp.nextInt(); } }

int b[][]=new int[r2][c2]; System.out.println("Enter into Second Matrix: "); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { System.out.print("\n Enter: "); b[i][j]=inp.nextInt(); } }

int pro[][]=new int[r1][c2]; for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { for(k=0;k<c1;k++) { pro[i][j]=pro[i][j]+(a[i][k]*b[k][j]); } } }

System.out.println("\n First Matrix: "); for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { System.out.print(a[i][j]+" "); } System.out.println(); }

System.out.println("\n Second Matrix: "); for(i=0;i<r2;i++) { for(j=0;j<c2;j++) { System.out.print(b[i][j]+" "); } System.out.println(); }

System.out.println("\n Product Matrix: "); for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { System.out.print(pro[i][j]+" "); } System.out.println(); }

} }

Output:

Enter Dimensions of First Matrix (Row * Column) : 
3
2

Enter Dimensions of Second Matrix (Row * Column) : 2 3 Enter into First Matrix:

Enter: 1

Enter: 2

Enter: 3

Enter: 4

Enter: 5

Enter: 6

Enter into Second Matrix:

Enter: 9

Enter: 8

Enter: 7

Enter: 6

Enter: 5

Enter: 4

First Matrix: 1 2 3 4 5 6

Second Matrix: 9 8 7 6 5 4

Product Matrix: 21 18 15 51 44 37 81 70 59

Java program to multiply two matrices