JAVA program to multiply two matrices
This JAVA program is to multiply two matrices.
For example, for a 2 x 2 matrix, the multiplication of two matrices matrix1 {1,2,3,4} and matrix2 {5,6,7,8} will be equal to mat{19,22,43,50}.
1 2 5 6 19 22
X =
3 4 7 8 43 50
Logic
In this, we multiply elements of matrix(1) and matrix(2)1st row and 1st column, 2nd row and 2nd column and so on and store the result in mul[][] matrix.To perform multiplication operation we use 3 for loops.
Dry Run of the Program
Take input mat1[][] and store elements in mat{1,2}{3,4}
Take input mat2[][] and store elements in mat{5,6}{7,8}
Take input ‘m’ and no of rows(m) for mat1 as 2
Take input ‘col’ and no of columns(n) for mat1 as 2
/*We do not take input for rows for mat2 as no of col of mat1=no of rows for mat2*/
Take input ‘col’ and no of columns(p) for mat2 as 2
1st iteration for(i=0;i<m;i++) i.e. for(i=0;0<2;i++) Outer loop
1st iteration for(j=0;j<p;j++) i.e. for(j=0;0<2;j++) Inner loop
1st iteration for(k=0;k<n;k++) i.e. for(k=0;0<2;k++) Innermost loop
mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j]; i.e mul[0][0]=mul[0][0]+mat1[0][0]*mat2[0][0]; i.e. mul[0][0]=0+1*5 i.e mul[0][0]=5
2nd iteration for(k=1;k<n;k++) i.e. for(k=1;1<2;k++) Innermost loop
mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j]; i.e mul[0][0]=mul[0][0]+mat1[0][1]*mat2[1][0]; i.e. mul[0][0]=5+2*7 i.e mul[0][0]=19
2nd iteration for(j=0;j<p;j++) i.e. for(j=1;1<2;j++) Inner loop
1st iteration for(k=0;k<n;k++) i.e. for(k=0;0<2;k++) Innermost loop
mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j]; i.e mul[0][1]=mul[0][1]+mat1[0][0]*mat2[0][1]; i.e. mul[0][1]=0+1*6 i.e mul[0][1]=6
2nd iteration for(k=1;k<n;k++) i.e. for(k=1;1<2;k++) Innermost loop
mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j]; i.e mul[0][1]=mul[0][1]+mat1[0][1]*mat2[1][1]; i.e. mul[0][1]=6+2*8 i.e mul[0][1]=22
/*keep doing this and we get elements for the second row*/
Hence the multiplication of two matrices we get mul[19,22][43,50].
Program
import java.util.*; class arr20 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int i,j,k,m,n,p; System.out.println("Enter the number of rows for 1st matrix"); m = sc.nextInt(); System.out.println("Enter the number of columns for 1st matrix"); n = sc.nextInt(); int[][] mat1 = new int[m][n]; System.out.println("Enter the elements of the 1st matrix") ; for(i=0;i<m;i++) { for(j=0;j<n;j++) { mat1[i][j] = sc.nextInt(); } } System.out.println("Enter the number of columns for 2nd matrix"); p = sc.nextInt(); int [][] mat2 = new int[n][p]; System.out.println("Enter the elements of the 2nd matrix") ; for(i=0;i<n;i++) { for(j=0;j<p;j++) { mat2[i][j] = sc.nextInt(); } } System.out.println("The elements of matrix1 are") ; for(i=0;i<m;i++) { for(j=0;j<n;j++) { System.out.print(mat1[i][j]+"\t"); } System.out.println(""); } System.out.println("The elements of matrix2 are") ; for(i=0;i<n;i++) { for(j=0;j<p;j++) { System.out.print(mat2[i][j]+"\t"); } System.out.println(""); } int mul[][] = new int [m][p]; System.out.println("\nMULTIPLICATION of elements of matrix1 and matrix2 are") ; for(i=0;i<m;i++) { for(j=0;j<p;j++) { mul[i][j] = 0; for(k=0;k<n;k++) { mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j]; } System.out.print(mul[i][j]+"\t"); } System.out.println(""); } } }