Java ProgramsPatternsPrint Continuous Number Pattern

Print Continuous Number Pattern in Java

beginner·  Patterns  ·  Number Patterns

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.

Input
5
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Java Program

Java
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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.

How It Works
  1. 1num starts at 1, declared outside both loops so it survives from one row into the next.
  2. 2The outer loop runs once per row, from 1 to n.
  3. 3The inner loop runs n times per row, each time printing the current num and then incrementing it.
  4. 4Because num is never reset between rows, row 2 continues exactly where row 1 left off.
For 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.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: The nested loop visits and prints each of the grid's n² cells exactly once, advancing a single counter variable.

Key Concepts

nested for looprunning counterrectangular grid

Approach 2: Java 8

Java
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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.

How It Works
  1. 1IntStream.rangeClosed(1, n) generates one stream element per row i.
  2. 2For each row, an inner IntStream.rangeClosed(1, n) generates that row's column positions j.
  3. 3.mapToObj(j -> String.valueOf((i - 1) * n + j)) computes each cell's number directly — (i - 1) * n is how many numbers every prior row already used, and j adds this row's own position.
  4. 4Collectors.joining(" ") joins that row's numbers before forEach prints it.
For row 2 (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.

Complexity
Time Complexity: O(n²)Space Complexity: O(1)

Why: The nested streams still compute one value per grid position across all rows, proportional to n², without collecting the full grid.

Key Concepts

StreamIntStream.rangeClosed()Collectors.joining()

Related Programs