Print Pyramid Pattern in Java
Problem
A centered pyramid needs two things a plain left-aligned triangle doesn't: leading spaces that shrink row by row to keep the shape centered, and an odd star count per row so it stays symmetric.
Given a height n, print a centered pyramid of stars with n rows.
Java Program
public class PyramidPattern {
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 < n - i; s++) row.append(" "); // leading spaces shrink as the pyramid widens
for (int j = 1; j <= 2 * i - 1; j++) {
if (j > 1) row.append(" ");
row.append("*");
}
System.out.println(row);
}
}
}Output
Core Logic
Shrinking the leading spaces and growing the star count together, row by row, keeps every row centered under the one below it.
- 1Row
i(from 1 to n) getsn - ileading spaces — the widest row, the last one, gets none. - 2That same row prints
2i - 1stars, always an odd count, which is what keeps the pyramid symmetric around its center. - 3Stars within a row are separated by a single space, the same convention used by the plain triangle pattern.
- 4As
iincreases, the leading spaces shrink while the star count grows, forming the sloped sides.
n = 5, row 3 gets 5 - 3 = 2 leading spaces and 2(3) - 1 = 5 stars.Key Point: The odd star count 2i - 1 is what makes centering possible at all — an even count could never sit symmetrically around a single middle column.
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 PyramidPatternStream {
public static void main(String[] args) {
int n = 5;
// Builds each row's leading spaces plus its stars, then prints it
IntStream.rangeClosed(1, n)
.mapToObj(i -> " ".repeat(n - i) + IntStream.rangeClosed(1, 2 * 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 stars 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(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"*"and joins them with spaces, the same odd-count star run the loop version builds. - 4
forEach(System.out::println)prints each completed row as it's produced.
" ".repeat(2) gives two leading spaces, and the inner stream joins five stars into * * * * *.Key Point: String.repeat() replaces the leading-space loop entirely, while the inner stream replaces the star-printing loop — together they turn two nested loops into two chained stream calls, the same technique already used for a centered number pyramid.
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.