Print Number Pyramid in Java
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.
Java Program
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
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.
- 1Row
igetsn - ileading spaces, the same shrinking-indentation technique used to center a star pyramid. - 2Row
ithen prints the numberiexactly2i - 1times, separated by single spaces. - 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.
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.
Why: Row i prints roughly 2i characters, and summed across all n rows that totals to a count proportional to n².
Key Concepts
Approach 2: Java 8
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
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.
- 1
IntStream.rangeClosed(1, n)produces one stream element per row numberi. - 2
" ".repeat(n - i)builds that row's leading spaces directly, without a separate loop. - 3An inner
IntStream.rangeClosed(1, 2 * i - 1)maps every position to the digitiand joins them with spaces, the same repeated-digit row the loop version builds. - 4
forEach(System.out::println)prints each completed row as it's produced.
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.
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.