-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathmatrix_multiplication.java
34 lines (31 loc) · 1.03 KB
/
matrix_multiplication.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
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] A = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] B = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int[][] RES = new int[3][3];
// Perform matrix multiplication
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
RES[i][j] += A[i][k] * B[k][j];
}
}
}
// Print the arrays
System.out.println("Matrix A:");
printMatrix(A);
System.out.println("Matrix B:");
printMatrix(B);
System.out.println("Result Matrix (A * B):");
printMatrix(RES);
}
// Helper method to print a matrix
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}