Print Inverted Number Pyramid in Java
Problem
An inverted number pyramid is the same repeated-row-number pyramid as its upright counterpart, just walked in the opposite row order so the widest row prints first and the apex ends up at the bottom.
Given a size n, print a centered n-row pyramid, upside down, where row i is filled entirely with the number i.
Java Program
public class InvertedNumberPyramid {
public static void main(String[] args) {
int n = 5;
for (int i = n; i >= 1; i--) { // widest row first, narrowest last
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(' ');
}
System.out.println(line);
}
}
}Output
Core Logic
Running the exact same per-row build used for the upright pyramid, but counting the row number down from n to 1 instead of up, flips which row prints first without changing how any individual row is built.
- 1The outer loop runs
ifromndown to1, instead of the upright pyramid's1up ton. - 2Each row still gets
n - ileading spaces and the digitirepeated2i - 1times — identical per-row logic to the upright version. - 3Because
istarts at its largest value, the widest row (n) prints first, and the narrowest row (1) prints last.
n = 5, the first row printed is i = 5 with no leading spaces, and the last row is i = 1 with 4 leading spaces — the exact reverse order of the upright pyramid.Key Point: Only the loop's direction changes here — the row-building logic (leading spaces, repeated digit, digit count) is copied unchanged from the upright pyramid, since flipping row order is enough to flip the whole shape.
Why: Row i still prints roughly 2i characters regardless of visiting order, so the total across all n rows scales with n², the same as the upright pyramid.
Key Concepts
Approach 2: Java 8
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class InvertedNumberPyramidStream {
public static void main(String[] args) {
int n = 5;
IntStream.rangeClosed(1, n)
.map(k -> n + 1 - k) // widest row first, narrowest last
.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
Reusing the upright pyramid's exact per-row formula, but feeding it row indices in descending order instead of ascending, flips the shape the same way reversing the loop does.
- 1
IntStream.rangeClosed(1, n).map(k -> n + 1 - k)produces the row indicesndown to1, the reverse of the upright pyramid's ascending stream. - 2
" ".repeat(n - i)and the innerIntStream.rangeClosed(1, 2 * i - 1)build each row exactly as the upright pyramid's stream does — nothing about a single row's construction changes. - 3
.mapToObj(j -> String.valueOf(i))repeats the row's own digit across that row's width. - 4
Collectors.joining(" ")joins that row's repeated digits beforeforEachprints it.
5, giving the widest row with no leading spaces; the last is 1, giving a single digit with the most leading spaces.Key Point: Only the row-index stream's direction changes here — the exact same per-row mapping function from the upright pyramid is reused unmodified, the same relationship the loop version has to its own upright counterpart.
Why: The nested streams still produce one value per grid position across all rows, proportional to n², regardless of visiting order.