Basic Looping in Java

Here is a simpler version, using decrementing loops and the ternary conditional operator to choose whether to print number or space.

for (int i = 5; i > 0; i--) {
    for (int j = 5; j > 0; j--)
        System.out.print(j > i ? " " : j);
    System.out.println();
}

Leave a Comment