-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmatrix_multiply.java
57 lines (49 loc) · 2.24 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
import java.util.Scanner;
public class matrix_multiply{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int sum = 0;
System.out.println("enter dimensions for A");
int rowsA = sc.nextInt();
int colsA = sc.nextInt(); //enter the dimensions of matrix A
int a[][] = new int[rowsA][colsA];
System.out.println("enter elements of A");
for(int i=0; i<rowsA; i++){
for(int j=0; j<colsA; j++){ //enter elements of matrix A
a[i][j] = sc.nextInt();
}
}
System.out.println("enter dimensions for B");
int rowsB = sc.nextInt();
int colsB = sc.nextInt(); //enter the dimensions of matrix B
int b[][] = new int[rowsB][colsB];
int c[][] = new int[rowsA][colsB];
if(colsA != rowsB){
System.out.println("These matrix can't be multiplied with each other."); //if rows of matrix B != columns of matrix A then multiplication not possible
}
else {
System.out.println("enter the elements of B");
for(int i=0; i<rowsB; i++){ //else enter the elements of matrix B
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++){ //logic for multiplication of 2 matrix
sum = sum + a[i][k]*b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
System.out.println("matrix A * matrix B");
for(int i=0; i<rowsA; i++){
for(int j=0; j<colsB; j++){ //print the multiplied matrix
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
}