First Way
class MatDiagSum{ public static void main(String args[]){ int size = 6; int mat[][] = new int[size][size]; // input ints in matrix for(int i=0;i(size-1)){ break; } } System.out.println("Sum of " + (size+i) + " diag is " + sum); nexti = size-1; nextj = i+2; sum=0; } } }
Second Way
class MatDiagSumOtherSide{ public static void main(String args[]){ int size = 6; int mat[][] = new int[size][size]; // input ints in matrix for(int i=0;i-1;i--){ for(int j=0;j(size-1)) { break; } } System.out.println("Sum of " + (size-i) + " diag is " + sum); nexti=0; nextj=i-1; sum=0; } // lets do remaining half-1 nexti = 1; nextj = 0; for(int i=0; i(size-1)){ break; } } System.out.println("Sum of " + (size+i) + " diag is " + sum); nexti = i+2; nextj = 0; sum=0; } } }