Java ProgramsPatternsPrint Number Pyramid

Print Number Pyramid in Java

intermediate·  Patterns  ·  Number Patterns

Problem

A number pyramid is a centered triangle where each row repeats its own row number enough times to fill that row's width, rather than counting up through different digits.

Given a size n, print a centered n-row pyramid where row i is filled entirely with the number i.

Input
5
Output
1 2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5

Java Program

Java
public class NumberPyramid { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) { StringBuilder line = new StringBuilder(); for (int j = 1; j <= n - i; j++) line.append(' '); for (int j = 1; j <= 2 * i - 1; j++) { line.append(i); if (j < 2 * i - 1) line.append(' '); // space between repeated digits, not after the last one } System.out.println(line); } } }

Output

1 2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5

Core Logic

Centering each row with leading spaces, then repeating that row's own number enough times to fill its width, builds the pyramid one row at a time.

How It Works
  1. 1Row i gets n - i leading spaces, the same shrinking-indentation technique used to center a star pyramid.
  2. 2Row i then prints the number i exactly 2i - 1 times, separated by single spaces.
  3. 3Because every value on a row is the same digit, no separate counting logic is needed to decide what to print next — it's always just i.
For row 3, the leading spaces are 5 - 3 = 2, followed by the digit 3 repeated 2 * 3 - 1 = 5 times: 3 3 3 3 3.
💡

Key Point: The width formula 2i - 1 is what keeps every row's total width growing at the same rate as a star pyramid's — only the character being repeated changes, not the shape.

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

Why: Row i prints roughly 2i characters, and summed across all n rows that totals to a count proportional to n².

Key Concepts

nested for loopleading spacesStringBuilder

Approach 2: Java 8

Java
import java.util.stream.Collectors; import java.util.stream.IntStream; public class NumberPyramidStream { public static void main(String[] args) { int n = 5; // Builds each row's leading spaces plus its repeated digits, then prints it IntStream.rangeClosed(1, n) .mapToObj(i -> " ".repeat(n - i) + IntStream.rangeClosed(1, 2 * i - 1) .mapToObj(j -> String.valueOf(i)) .collect(Collectors.joining(" "))) .forEach(System.out::println); } }

Output

1 2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5

Core Logic

Each row can be built independently as a string — leading spaces plus a joined run of repeated digits — which a stream can generate and print row by row.

How It Works
  1. 1IntStream.rangeClosed(1, n) produces one stream element per row number i.
  2. 2" ".repeat(n - i) builds that row's leading spaces directly, without a separate loop.
  3. 3An inner IntStream.rangeClosed(1, 2 * i - 1) maps every position to the digit i and joins them with spaces, the same repeated-digit row the loop version builds.
  4. 4forEach(System.out::println) prints each completed row as it's produced.
For row 3, " ".repeat(2) gives two leading spaces, and the inner stream joins five copies of "3" into 3 3 3 3 3.
💡

Key Point: String.repeat() replaces the leading-space loop entirely, while the inner stream replaces the digit-repeating loop — together they turn two nested loops into two chained stream calls.

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

Why: The stream still produces and joins roughly 2i characters per row, the same total work as the loop version, without collecting more than one row at a time.

Key Concepts

StreamIntStream.rangeClosed()Collectors.joining()

Related Programs