forked from PrajaktaSathe/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix_Multiply.java
64 lines (64 loc) · 1.75 KB
/
Matrix_Multiply.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Program to multply two matrices in Java
package matrix;
import java.util.Scanner;
public class Matrix_Multiply {
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in)) {
System.out.println("Enter the row and column number for 1st matrix");
int n=sc.nextInt();
int m=sc.nextInt();
// Input for the row and column number
int a1[][] = new int[n][m];
// New 2D array for first matrix
System.out.println("Enter "+(n*m)+ " numbers");
// Taking input of the first matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a1[i][j] = sc.nextInt();
}
}
int n1=sc.nextInt();
int m1=sc.nextInt();
// Input for the row and column number
int a2[][] = new int[n1][m1];
// New 2D array for second matrix
System.out.println("Enter "+(n1*m1)+ " numbers");
// Taking input of the second matrix
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m1; j++) {
a2[i][j] = sc.nextInt();
}
}
if(m!=n1)
{
System.out.println("Invalid coordinates of the matrix");
return;
}
int multiply[][] = new int[n][m1];
// New 2D array for multiplication of the two matrices -
System.out.println("The multiplication of two matrices are - ");
// Performing multiplication of two matrices -
for(int i=0;i<n;i++)
{
for(int j=0;j<m1;j++)
{
multiply[i][j]=0;
for(int d=0;d<m;d++)
{
multiply[i][j]=multiply[i][j]+(a1[i][d]*a2[d][j]);
// multiplication method
}
}
}
// Displaying multiplication of two matrices -
for(int i=0;i<n;i++)
{
for(int j=0;j<m1;j++)
{
System.out.print(multiply[i][j]+" ");
}
System.out.println();
}
}
}
}