Table of Contents
Write a Java program for multiplying two matrices and print the product for the same
A Java program for multiplying two matrices.
Java program to multiply two matrices
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 39 40 41 42 43 44 45 | import java.util.Scanner; //itvoyagers.in class MatrixMultiplication { public static int[][] matrix() { Scanner s = new Scanner(System.in); int m[][]= new int[2][2]; for(int i=0; i < 2; i++) { for(int j=0; j < 2; j++) { m[i][j] = s.nextInt(); System.out.print("t"); } System.out.print("n"); } return m; } //itvoyagers.in public static void main(String[] args) { int a[][] = new int[2][2]; int b[][] = new int[2][2]; int res[][] = new int[2][2]; System.out.println("n Enter values for first matrix. t"); a = matrix(); System.out.println("n Enter values for second matrix. t"); b = matrix(); //itvoyagers.in System.out.println("n Resultant matrix. t"); for(int i=0; i < 2; i++) { for(int j=0; j < 2; j++) { for(int k=0; k < 2; k++) { res[i][j] += a[i][k] * b[k][j]; } System.out.print("t" + res[i][j] + "t"); } System.out.print("n"); } }//itvoyagers.in } |
Output

PRACTICALS/PRACTICE PROGRAM IN Java
CHECKOUT OTHER RELATED TOPICS
We are aiming to explain all concepts of Java in easiest terms as possible.

ITVoyagers
Author