-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiagnolSum.java
30 lines (28 loc) · 912 Bytes
/
DiagnolSum.java
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
public class DiagnolSum {
public static void Sum(int matrix[][]) {
int sum = 0;
//for (int i = 0; i <= matrix.length; i++) {
//for (int j = 0; j <= matrix[0].lenght; j++) {
//if (i == j) {
//sum += matrix[i][j]; // we got the sum of primary diagnol
//} else if (i + j == matrix.length - 1) {
//sum += matrix[i][j];
//}
//}
//}
//return sum;
for (int i = 0; i < matrix.length; i++) {
sum += matrix[i][i];
if (i != matrix.length - 1 - i) {
sum += matrix[i][matrix.length - i - 1];
}
}
System.out.print(sum);
}
public static void main(String args[]) {
int matrix[][] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
Sum(matrix);
}
}