I'm new to java. How can I find the sum of diagonals (that looks like /) in a 2D Matrix?
Let's say the matrix looks something like this:
4 6 7
2 4 7
5 2 7
The sum of the diagonal(that looks like \) is 15 (4+4+7). Here's my code for that diagonal:
public static int Diagonal(int m[][]) {
int sum = 0;
for(int row = 0; row < m[0].length; row++) {
sum += m[row][row];
}
return sum;
}
How can I find the sum of the other diagonal that looks like (/)?