OneCompiler

2nd Diagonal of 2d Array Matrix

1647

public class Main {
public static void main(String[] args) {
int i, j;

    int a[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    System.out.println("Second diagonal of matrix:");
    
    for (i = 0; i < 3; i++) { 
        for (j = 0; j < 3; j++) { // Change to < 3
            if (i + j == 2) {
                System.out.print(a[i][j] + " "); // Add space for better readability
            }
        }
    }
    System.out.println(); // Move to the next line after printing
}

}