-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMatrix.java
More file actions
44 lines (39 loc) · 1.29 KB
/
Matrix.java
File metadata and controls
44 lines (39 loc) · 1.29 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
public class Matrix {
public static void main(String[] args) {
int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] matrix2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};
int rows = matrix1.length;
int cols = matrix1[0].length;
int[][] result = new int[rows][cols];
// Multiply matrices
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
for (int k = 0; k < cols; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
// Print result
System.out.println("Result of matrix multiplication:");
for (int[] row : result) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
// Matrix Addition
int[][] sum = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
System.out.println("\nResult of matrix addition:");
for (int[] row : sum) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}