This code example prints a pattern of numbers. The program uses nested for loops to iterate over the rows and columns of the pattern and prints the value of i
for each position in the pattern.
class Pattern {
public static void main(String[] args) {
int i, j;
for (i = 1; i <= 8; i++) {
for (j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
}
}
1
22
333
4444
55555
666666
7777777
88888888
Here’s a breakdown of how the program works:
i
.i
is printed using the System.out.print()
method.System.out.println()
method to move to the next row.