-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
25 lines (23 loc) · 834 Bytes
/
Matrix.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
public class Matrix {
public int[] sumDiagonal(int[][] n) {
int suml = 0;
int sumr = 0;
for (int i = 0; i < n.length; i++) {
for (int j = 0; j < n[0].length; j++) {
if (i == j)
suml += n[i][j];
if (i + j == n.length)
sumr += n[i][j];
}
}
return new int[] { suml, sumr };
}
public static void main(String[] args) {
Matrix obj = new Matrix();
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]);
}
}