38 lines
		
	
	
	
		
			1.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			38 lines
		
	
	
	
		
			1.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
import java.util.Scanner;
 | 
						|
 | 
						|
class NonDiagEle {
 | 
						|
 | 
						|
    public static void main(String args[]) {
 | 
						|
        Scanner sc = new Scanner(System.in);
 | 
						|
        System.out.print("Enter the dimension of the square matrix: ");
 | 
						|
        int n = sc.nextInt();
 | 
						|
        int a[][] = new int[n][n];
 | 
						|
 | 
						|
        for (int i = 0; i < n; i++) {
 | 
						|
            for (int j = 0; j < n; j++) {
 | 
						|
                System.out.print(
 | 
						|
                    "Enter the element at (" + i + "," + j + "): "
 | 
						|
                );
 | 
						|
                a[i][j] = sc.nextInt();
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        System.out.println();
 | 
						|
        int nonDiagSum = 0;
 | 
						|
 | 
						|
        for (int i = 0; i < n; i++) {
 | 
						|
            for (int j = 0; j < n; j++) {
 | 
						|
                if (i != j && i + j != n - 1) {
 | 
						|
                    System.out.print(a[i][j] + " ");
 | 
						|
                    nonDiagSum += a[i][j];
 | 
						|
                } else {
 | 
						|
                    System.out.print("X ");
 | 
						|
                }
 | 
						|
            }
 | 
						|
            System.out.println();
 | 
						|
        }
 | 
						|
 | 
						|
        System.out.println();
 | 
						|
        System.out.println("Sum of Non-Diagonal Elements: " + nonDiagSum);
 | 
						|
    }
 | 
						|
}
 |