This works:
public class Main {
public static void main(String args[]) {
int n = 6;
while (n > 0) {
for (int i = 1; i <= n; i++) {
System.out.print(i);
}
for (int i = n; i > 0; i--) {
System.out.print(i);
}
System.out.println("");
n--;
}
}
}
The second for
loop assigns n
to the iterator int i
, executes the statements within the curly brackets, then decrements i
using i--
until the condition i > 0
is no longer true.
3
solved Printing triangles using nested loop in Java [closed]