Facebook Pixel

Number Pattern Basics: Printing Ascending Numbers

Transition from star patterns to number patterns, learning how to utilize loop counters to print dynamic values.

From Shapes to Data

Printing stars teaches you about grid boundaries. Printing numbers introduces a new concept: utilizing your loop counter variables as actual data.

The Column Variable Pattern

What if we want to print this?

1
1 2
1 2 3

Look at the data being printed. The numbers reset every row, and they match the exact column index. Therefore, you simply print the inner loop variable (col).

for (int row = 1; row <= n; row++) {
    for (int col = 1; col <= row; col++) {
        System.out.print(col + " ");
    }
    System.out.println();
}

The Row Variable Pattern

What if the data looks like this?

1
2 2
3 3 3

The number stays constant throughout the entire row, and matches the row index. You print the outer loop variable (row).

for (int row = 1; row <= n; row++) {
    for (int col = 1; col <= row; col++) {
        System.out.print(row + " ");
    }
    System.out.println();
}

The Continuous Counter Pattern

What if the numbers never reset?

1
2 3
4 5 6

Neither row nor col works here because they reset. You must declare an independent counter variable outside the loops.

int count = 1;
for (int row = 1; row <= n; row++) {
    for (int col = 1; col <= row; col++) {
        System.out.print(count + " ");
        count++; // Increment independently
    }
    System.out.println();
}

The Takeaway

Number patterns require you to analyze the behavior of the data. Does it reset every row? (Print col). Does it stay constant? (Print row). Does it never reset? (Use an external variable).

In star patterns, you print a static character. In number patterns, you must dynamically print the correct variable (row, col, or an external counter) based on the data's behavior.

If the sequence is 1, 1 2, 1 2 3, the data perfectly matches the inner loop's column counter. You simply print the 'col' variable.

If a row is 3 3 3, the data matches the outer loop's row counter. You print the 'row' variable.

It is a pattern (like Floyd's Triangle) where numbers increase continuously without resetting. This requires creating a state variable outside the loops and incrementing it after every print.

Numbers, unlike single stars, can have multiple digits (like 10). Adding a space (e.g., print(count + ' ')) ensures the numbers don't blur together into a single unreadable block.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.