- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
Latest commit
25 lines (23 loc) · 834 Bytes
/
Copy pathMatrix.java
File metadata and controls
25 lines (23 loc) · 834 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
//Link: https://onecompiler.com/java/3xctp6nec
publicclassMatrix {
publicint[] sumDiagonal(int[][] n) {
intsuml = 0;
intsumr = 0;
for (inti = 0; i < n.length; i++) {
for (intj = 0; j < n[0].length; j++) {
if (i == j)
suml += n[i][j];
if (i + j == n.length)
sumr += n[i][j];
}
}
returnnewint[] { suml, sumr };
}
publicstaticvoidmain(String[] args) {
Matrixobj = newMatrix();
int[][] nums = { { 1, 2, 3, 4 }, { 1, 2, 3, 4 }, { 1, 2, 3, 4 }, { 1, 2, 3, 4 } };
System.out.print("Sum of left Diagonal: " + obj.sumDiagonal(nums)[0]);
System.out.println();
System.out.print("Sum of right Diagonal: " + obj.sumDiagonal(nums)[1]);
}
}