Print Inverted Pyramid in Java
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.
Java Program
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.
- 1Row
i(from 1 to n) getsi - 1leading spaces — the first row, the widest, gets none. - 2That same row prints
2(n - i) + 1stars, an odd count that shrinks asiincreases. - 3Stars within a row are separated by a single space, the same convention used by the upright pyramid.
- 4As
iincreases, the leading spaces grow while the star count shrinks, forming the downward-pointing sides.
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.
Why: The total characters (spaces plus stars) printed across all rows is proportional to n², with no growing storage.
Key Concepts
Approach 2: Java 8
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.
- 1
IntStream.rangeClosed(1, n)produces one stream element per row numberi. - 2
" ".repeat(i - 1)builds that row's leading spaces, growing asiincreases — the reverse of the upright pyramid's shrinking spaces. - 3An inner
IntStream.rangeClosed(1, 2 * (n - i) + 1)maps every position to"*"and joins them, producing that row's shrinking star count. - 4
forEach(System.out::println)prints each completed row as it's produced.
" ".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.
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.