This is a simple java program that teaches you for multiplying two matrix to each other. We are going to make a simple program that will multiply two matrix. Two dimensional array represents the matrix.

Now, make this program, you have to declare two multidimensional array of type integer. Program uses two for loops to get number of rows and columns by using the **arrayA.length**. After getting both matrix then multiply to it. Both matrix will be multiplied to each other by using ‘**for’** loop. So the output will be displayed on the screen command prompt by using the **print()** method.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
class MatMux { public static void main(String[] args) { int arrayA[][] = {{5,6,7,9},{1,1,1,1},{1,2,3,4},{0,0,5,6}}; /* 4x4 matrix*/ int arrayB[][] = {{0,1},{0,2},{0,4},{1,5}};/* 4x2 matrix */ int arrayResult[][] = new int[10][10]; int x = arrayA.length; System.out.println("Array A"); for(int i=0;i<x;i++) { for(int j=0;j<x;j++) { System.out.print(" " +arrayA[i][j]); } System.out.println(); } int y = arrayB.length; System.out.println("Array B"); for(int i=0;i<y;i++) { for(int j=0;j<y-2;j++) { System.out.print(" "+arrayB[i][j]); } System.out.println(); } System.out.println("Product Matrix"); for(int i=0;i < x;i++) { for(int j=0;j<y-2;j++) { for(int k=0;k<x;k++) { arrayResult[i][j] += arrayA[i][k]*arrayB[k][j]; } } } for(int i=0;i<x;i++) { for(int j=0;j<y-2;j++) { System.out.print(" "+arrayResult[i][j]); } System.out.println(); } } } |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Array A 5 6 7 9 1 1 1 1 1 2 3 4 0 0 5 6 Array B 0 1 0 2 0 4 1 5 Product Matrix 9 90 1 12 4 37 6 50 |