Java ProgramsPatternsPrint Pyramid Pattern

Print Pyramid Pattern in Java

intermediate·  Patterns  ·  Star Patterns

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.

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

Java Program

Java
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.

How It Works
  1. 1Row i (from 1 to n) gets n - i leading spaces — the widest row, the last one, gets none.
  2. 2That same row prints 2i - 1 stars, always an odd count, which is what keeps the pyramid symmetric around its center.
  3. 3Stars within a row are separated by a single space, the same convention used by the plain triangle pattern.
  4. 4As i increases, the leading spaces shrink while the star count grows, forming the sloped sides.
For 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.

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 spacesodd star counts

Approach 2: Java 8

Java
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.

How It Works
  1. 1IntStream.rangeClosed(1, n) produces one stream element per row number i.
  2. 2" ".repeat(n - i) builds that row's leading spaces directly, without a separate loop.
  3. 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. 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 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.

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

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.

Key Concepts

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

Related Programs