MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / main

Method main

Programs/matrix/Matrix_Multiply.java:5–63  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

3import java.util.Scanner;
4public class Matrix_Multiply {
5 public static void main(String[] args) {
6 try (Scanner sc = new Scanner(System.in)) {
7 System.out.println("Enter the row and column number for 1st matrix");
8 int n=sc.nextInt();
9 int m=sc.nextInt();
10 // Input for the row and column number
11 int a1[][] = new int[n][m];
12 // New 2D array for first matrix
13 System.out.println("Enter "+(n*m)+ " numbers");
14 // Taking input of the first matrix
15 for (int i = 0; i < n; i++) {
16 for (int j = 0; j < m; j++) {
17 a1[i][j] = sc.nextInt();
18 }
19 }
20 int n1=sc.nextInt();
21 int m1=sc.nextInt();
22 // Input for the row and column number
23 int a2[][] = new int[n1][m1];
24 // New 2D array for second matrix
25 System.out.println("Enter "+(n1*m1)+ " numbers");
26 // Taking input of the second matrix
27 for (int i = 0; i < n1; i++) {
28 for (int j = 0; j < m1; j++) {
29 a2[i][j] = sc.nextInt();
30 }
31 }
32 if(m!=n1)
33 {
34 System.out.println("Invalid coordinates of the matrix");
35 return;
36 }
37 int multiply[][] = new int[n][m1];
38 // New 2D array for multiplication of the two matrices -
39 System.out.println("The multiplication of two matrices are - ");
40 // Performing multiplication of two matrices -
41 for(int i=0;i<n;i++)
42 {
43 for(int j=0;j<m1;j++)
44 {
45 multiply[i][j]=0;
46 for(int d=0;d<m;d++)
47 {
48 multiply[i][j]=multiply[i][j]+(a1[i][d]*a2[d][j]);
49 // multiplication method
50 }
51 }
52 }
53 // Displaying multiplication of two matrices -
54 for(int i=0;i<n;i++)
55 {
56 for(int j=0;j<m1;j++)
57 {
58 System.out.print(multiply[i][j]+" ");
59 }
60 System.out.println();
61 }
62 }

Callers

nothing calls this directly

Calls 1

printMethod · 0.80

Tested by

no test coverage detected