Print Continuous Number Pattern in Java
Problem
A continuous number pattern is a plain rectangular grid, not a growing triangle — the numbers just keep incrementing from one row straight into the next, never resetting back to 1.
Given a grid size n, fill an n by n grid with the numbers 1 through n squared, counting up continuously row by row.
Java Program
public class ContinuousNumberPattern {
public static void main(String[] args) {
int n = 5;
int num = 1; // survives across rows, never resets
for (int i = 1; i <= n; i++) {
StringBuilder row = new StringBuilder();
for (int j = 1; j <= n; j++) {
if (j > 1) row.append(" ");
row.append(num++);
}
System.out.println(row);
}
}
}Output
Core Logic
Keeping a single counter outside both loops, and incrementing it once per printed number regardless of which row it's in, lets the numbering carry straight through every row boundary.
- 1
numstarts at1, declared outside both loops so it survives from one row into the next. - 2The outer loop runs once per row, from
1ton. - 3The inner loop runs
ntimes per row, each time printing the currentnumand then incrementing it. - 4Because
numis never reset between rows, row 2 continues exactly where row 1 left off.
n = 5, row 1 uses 1 through 5, and row 2 doesn't restart — it continues straight on with 6 through 10.Key Point: This is a fixed n-by-n rectangle, not a growing triangle — every row has exactly n numbers, unlike Number Triangle or Floyd's Triangle, where each row is wider than the last.
Why: The nested loop visits and prints each of the grid's n² cells exactly once, advancing a single counter variable.
Key Concepts
Approach 2: Java 8
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class ContinuousNumberPatternStream {
public static void main(String[] args) {
int n = 5;
IntStream.rangeClosed(1, n)
.mapToObj(i -> IntStream.rangeClosed(1, n)
.mapToObj(j -> String.valueOf((i - 1) * n + j))
.collect(Collectors.joining(" ")))
.forEach(System.out::println);
}
}
Output
Core Logic
Since a fixed n-by-n grid means every cell's continuous number can be computed directly from its row and column, (i - 1) * n + j reproduces the running counter without ever incrementing a shared variable.
- 1
IntStream.rangeClosed(1, n)generates one stream element per rowi. - 2For each row, an inner
IntStream.rangeClosed(1, n)generates that row's column positionsj. - 3
.mapToObj(j -> String.valueOf((i - 1) * n + j))computes each cell's number directly —(i - 1) * nis how many numbers every prior row already used, andjadds this row's own position. - 4
Collectors.joining(" ")joins that row's numbers beforeforEachprints it.
i = 2), (2 - 1) * 5 = 5, so column j = 1 maps to 6 — exactly where the loop version's shared counter would be after finishing row 1.Key Point: The row-major index formula (i - 1) * n + j replaces the shared, ever-incrementing counter entirely — each cell computes its own number independently, with no state carried between positions.
Why: The nested streams still compute one value per grid position across all rows, proportional to n², without collecting the full grid.