-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiplication.java
More file actions
44 lines (44 loc) · 1.24 KB
/
Copy pathMatrixMultiplication.java
File metadata and controls
44 lines (44 loc) · 1.24 KB
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
import java.util.*;
import java.lang.*;
public class MatrixMultiplication {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows1 and coloumns1");
int r1 = sc.nextInt();
int c1 = sc.nextInt();
int a[][] = new int[r1][c1];
for(int i = 0;i<r1;i++){
for(int j = 0;j<c1;j++){
a[i][j] = sc.nextInt();
}
}
System.out.println("Enter the number of rows2 and coloumns2");
int r2 = sc.nextInt();
int c2 = sc.nextInt();
int b[][] = new int[r2][c2];
for(int i = 0;i<r2;i++){
for(int j = 0;j<c2;j++){
b[i][j] = sc.nextInt();
}
}
if(r1 == c2){
int c[][] = new int[r1][c2];
for(int i = 0;i<r1;i++){
for(int j = 0;j<c2;j++){
for(int k = 0;k<c1;k++){
c[i][j] += a[i][k] * b[k][j];
}
}
}
for(int i = 0;i<r1;i++){
for(int j = 0;j<c2;j++){
System.out.print(+c[i][j] +" ");
}
System.out.println();
}
}
else{
System.out.println("MAtrix multiplication is not possible");
}
}
}