Java ProgramsPatternsPrint Inverted Number Pyramid

Print Inverted Number Pyramid in Java

intermediate·  Patterns  ·  Number Patterns

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.

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

Java Program

Java
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

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

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.

How It Works
  1. 1The outer loop runs i from n down to 1, instead of the upright pyramid's 1 up to n.
  2. 2Each row still gets n - i leading spaces and the digit i repeated 2i - 1 times — identical per-row logic to the upright version.
  3. 3Because i starts at its largest value, the widest row (n) prints first, and the narrowest row (1) prints last.
For 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.

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

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

nested for loopdecrementing counterStringBuilder

Approach 2: Java 8

Java
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

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

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.

How It Works
  1. 1IntStream.rangeClosed(1, n).map(k -> n + 1 - k) produces the row indices n down to 1, the reverse of the upright pyramid's ascending stream.
  2. 2" ".repeat(n - i) and the inner IntStream.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. 3.mapToObj(j -> String.valueOf(i)) repeats the row's own digit across that row's width.
  4. 4Collectors.joining(" ") joins that row's repeated digits before forEach prints it.
The first row index produced is 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.

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

Why: The nested streams still produce one value per grid position across all rows, proportional to n², regardless of visiting order.

Key Concepts

StreamIntStream.rangeClosed()Collectors.joining()

Related Programs