-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiply2dMatrices.java
49 lines (47 loc) · 1.03 KB
/
multiply2dMatrices.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
package practise;
import java.util.Arrays;
import java.util.Scanner;
public class multiply2dMatrices {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int rowsA = sc.nextInt();
int colsA = sc.nextInt();
int a[][] = new int[rowsA][colsA];
for(int i=0; i<rowsA; i++) {
for(int j=0; j<colsA; j++) {
a[i][j]=sc.nextInt();
}
}
int rowsB = sc.nextInt();
int colsB = sc.nextInt();
int b[][] = new int[rowsB][colsB];
int c[][] = new int[rowsA][colsB];
if(colsA!=rowsB) {
System.out.println("Not applicable");
}
else {
int res = 0;
for(int i=0; i<rowsB; i++) {
for(int j=0; j<colsB; j++) {
b[i][j]=sc.nextInt();
}
}
for(int i=0; i<rowsA; i++) {
for(int j=0; j<colsB; j++) {
for(int k=0; k<rowsB; k++) {
res = res + a[i][k]*b[k][j];
}
c[i][j]=res;
res=0;
}
}
for(int i=0; i<rowsA; i++) {
for(int j=0; j<colsB; j++) {
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
sc.close();
}
}