Java ProgramsPatternsPrint Inverted Pyramid

Print Inverted Pyramid in Java

intermediate·  Patterns  ·  Star Patterns

Problem

An inverted pyramid is a centered pyramid with its rows visited in the opposite order — the widest row comes first and the single-star apex comes last.

Given a height n, print a centered pyramid of stars upside down, with n rows.

Input
n = 5
Output
* * * * * * * * * * * * * * * * * * * * * * * * *

Java Program

Java
public class InvertedPyramid { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) { StringBuilder row = new StringBuilder(); for (int s = 0; s < i - 1; s++) row.append(" "); // leading spaces grow as the pyramid narrows for (int j = 1; j <= 2 * (n - i) + 1; j++) { if (j > 1) row.append(" "); row.append("*"); } System.out.println(row); } } }

Output

* * * * * * * * * * * * * * * * * * * * * * * * *

Core Logic

Growing the leading spaces and shrinking the star count together, row by row, keeps every row centered while narrowing toward a single point at the bottom.

How It Works
  1. 1Row i (from 1 to n) gets i - 1 leading spaces — the first row, the widest, gets none.
  2. 2That same row prints 2(n - i) + 1 stars, an odd count that shrinks as i increases.
  3. 3Stars within a row are separated by a single space, the same convention used by the upright pyramid.
  4. 4As i increases, the leading spaces grow while the star count shrinks, forming the downward-pointing sides.
For n = 5, row 3 gets 3 - 1 = 2 leading spaces and 2(5 - 3) + 1 = 5 stars.
💡

Key Point: This is the upright pyramid's own rows in reverse — row 1 here matches the upright pyramid's last row, and row n here matches its first.

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

Why: The total characters (spaces plus stars) printed across all rows is proportional to n², with no growing storage.

Key Concepts

nested for loopleading spacesreversed row order

Approach 2: Java 8

Java
import java.util.stream.Collectors; import java.util.stream.IntStream; public class InvertedPyramidStream { public static void main(String[] args) { int n = 5; IntStream.rangeClosed(1, n) .mapToObj(i -> " ".repeat(i - 1) + IntStream.rangeClosed(1, 2 * (n - i) + 1) .mapToObj(j -> "*") .collect(Collectors.joining(" "))) .forEach(System.out::println); } }

Output

* * * * * * * * * * * * * * * * * * * * * * * * *

Core Logic

Building each row's leading spaces with String.repeat() and its shrinking star run with a joined inner stream turns the two nested loops into two chained expressions per row.

How It Works
  1. 1IntStream.rangeClosed(1, n) produces one stream element per row number i.
  2. 2" ".repeat(i - 1) builds that row's leading spaces, growing as i increases — the reverse of the upright pyramid's shrinking spaces.
  3. 3An inner IntStream.rangeClosed(1, 2 * (n - i) + 1) maps every position to "*" and joins them, producing that row's shrinking star count.
  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 2 * (5 - 3) + 1 = 5 stars into * * * * *.
💡

Key Point: Both formulas — leading spaces and star count — are swapped from the upright pyramid's, the same pair of changes the loop version makes; the stream structure itself is otherwise identical.

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

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

Key Concepts

StreamString.repeat()IntStream.rangeClosed()Collectors.joining()

Related Programs