-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplicationmatrix.java
56 lines (53 loc) · 1.79 KB
/
multiplicationmatrix.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
import java.util.Scanner;
public class multiplicationmatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("enter row and column matrix 1");
int r1 = sc.nextInt();
int c1 = sc.nextInt();
int[][] vib = new int[r1][c1];
for (int i = 0; i < r1; i++) { // row
for (int j = 0; j <c1; j++) { // column
vib[i][j] = sc.nextInt();
}
}
System.out.println("enter row and column matr ix 2");
int r2 = sc.nextInt();
int c2 = sc.nextInt();
int[][] vibh = new int[r2][c2];
for (int i = 0; i < r2; i++) { // row
for (int j = 0; j <c2; j++) { // column
vibh[i][j] = sc.nextInt();
}
}
System.out.println("matrix 1");
print(vib);
System.out.println("matrix 2");
print(vibh);
mult(vib,r1,c1,vibh ,r2,c2);
}
static void print(int[][] vib) {
for (int i = 0; i < vib.length; i++) { // row
for (int j = 0; j < vib[i].length; j++) {// column
System.out.print(vib[i][j] + " ");
}
System.out.println();
}
}
static void mult(int [][] vib,int r1, int c1, int [][]vibh,int r2,int c2) {
if (r2 != c1 ) {
System.out.println("wrong input");
return;
}
int [][] multiply = new int [r1][c2];
for (int i = 0 ; i <r1; i++) {
for (int j =0; j <c2; j ++) {
for (int k = 0; k < r2; k++) {
multiply[i][j] += (vib[i][k] * vibh[k][j]);
}
}
}
System.out.println("multiply");
print(multiply);
}
}