-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixAddition.java
More file actions
31 lines (27 loc) · 882 Bytes
/
Copy pathMatrixAddition.java
File metadata and controls
31 lines (27 loc) · 882 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
public class MatrixAddition {
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[][] result = addMatrices(a, b);
System.out.println("Resultant Matrix:");
printMatrix(result);
}
static int[][] addMatrices(int[][] a, int[][] b) {
int rows = a.length, cols = a[0].length;
int[][] result = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
return result;
}
static void printMatrix(int[][] matrix) {
for (int[] row : matrix) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}