-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix_Multiplication (1).java
57 lines (45 loc) · 1.6 KB
/
Matrix_Multiplication (1).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
import java.util.Scanner;
public class Matrix_Multiplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of a row in a Ist Matrix: ");
int n = sc.nextInt();
System.out.print("Enter the size of a coloumn in a Ist Matrix: ");
int m = sc.nextInt();
int[][] a = new int[n][m];
System.out.print("Enter the elements in a Ist matrix");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.print("Enter the size of a row in a Ist Matrix: ");
int o = sc.nextInt();
System.out.print("Enter the size of a coloumn in a Ist Matrix: ");
int p = sc.nextInt();
int[][] b = new int[o][p];
System.out.print("Enter the elements in a matrix");
for (int i = 0; i < o; i++) {
for (int j = 0; j < m; j++) {
b[i][j] = sc.nextInt();
}
}
if (m == o) {
int[][] c = new int[n][p];
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < o;k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
System.out.println(c[i][j]);
}
}
}
sc.close();
;
}
}