-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiplication.java
More file actions
33 lines (29 loc) · 964 Bytes
/
Copy pathMatrixMultiplication.java
File metadata and controls
33 lines (29 loc) · 964 Bytes
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
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] a = {{1, 2}, {3, 4}};
int[][] b = {{5, 6}, {7, 8}};
int[][] result = multiplyMatrices(a, b);
System.out.println("Resultant Matrix:");
printMatrix(result);
}
static int[][] multiplyMatrices(int[][] a, int[][] b) {
int rowsA = a.length, colsA = a[0].length, colsB = b[0].length;
int[][] result = new int[rowsA][colsB];
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
return result;
}
static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}