Untitled

 avatar
unknown
plain_text
3 years ago
825 B
8
Indexable
import java.util.Scanner;

public class SumOfDiagonals{
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);

        int arraySize = sc.nextInt();

        int twoD[][] = new int[arraySize][arraySize];

        sc.close();

        int total1 = 0;

        for(int i = 0; i<twoD.length; i++){
            for(int x = 0; x<twoD[i].length; x++){
                if(i == x){
                    total1 += twoD[i][x];
                }
            }
        }
    
        int total2 = 0;

        for(int i = 0; i<twoD.length; i++){
            for(int x = 0; i<twoD[i].length; x++){
                if((i+x) == (arraySize - 1)){
                    total2 += twoD[i][x];
                }
            }
        }
        System.out.println("Sum of diagonals = " + (total1 + total2));
    }
}
Editor is loading...