-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiplication.java
More file actions
59 lines (47 loc) · 1.85 KB
/
MatrixMultiplication.java
File metadata and controls
59 lines (47 loc) · 1.85 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)){
System.out.println("Enter the number of rows of Matrix A");
int rowsA = scan.nextInt();
System.out.println("Enter the number of columns of matrix A");
int colsA = scan.nextInt();
System.out.println("Enter the number of rows of Matrix B");
int rowsB = scan.nextInt();
System.out.println("Enter the number of columns of Matrix B");
int colsB = scan.nextInt();
int[][] a = new int [rowsA] [colsA];
int[][] b = new int [rowsB] [colsB];
int[][] c = new int [rowsA] [colsB];
System.out.println("Enter the elements of matrix A");
for(int i=0; i<rowsA; i++){
for(int j=0; j<colsA; j++){
a[i][j] = scan.nextInt();
}
}
System.out.println("Enter the elements of matrix B");
for(int i=0; i<rowsB; i++){
for(int j=0; j<colsB; j++){
b[i][j] = scan.nextInt();
}
}
for(int i=0; i<rowsA; i++){
for(int j=0; j<colsB; j++){
for(int k=0; k<colsA; k++){
c[i][j] = a[i][k]*b[k][j];
}
}
}
System.out.println("the matrix multiplication is ");
for(int i=0; i<rowsA; i++){
for(int j=0; j<colsB; j++){
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
catch(Exception e){
System.out.println("Please enter Numbers as input");
}
}
}