-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixOperations.java
More file actions
50 lines (44 loc) · 1.27 KB
/
Copy pathMatrixOperations.java
File metadata and controls
50 lines (44 loc) · 1.27 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
public class MatrixOperations {
public static void main(String[] args) {
int[][] A = {
{1, 2},
{3, 4}
};
int[][] B = {
{5, 6},
{7, 8}
};
int[][] sum = new int[2][2];
int[][] multiply = new int[2][2];
// Matrix Addition
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum[i][j] = A[i][j] + B[i][j];
}
}
// Matrix Multiplication
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
multiply[i][j] += A[i][k] * B[k][j];
}
}
}
// Print Addition
System.out.println("Matrix Addition:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
// Print Multiplication
System.out.println("\nMatrix Multiplication:");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(multiply[i][j] + " ");
}
System.out.println();
}
}
}