Java ProgramsPatternsPrint Alphabet Pyramid

Print Alphabet Pyramid in Java

intermediate·  Patterns  ·  Character Patterns

Problem

A centered pyramid needs two things per row — leading spaces to center it and a repeated symbol to fill it — and swapping a star for a fixed letter per row only changes the second part.

Given a number of rows, print a centered pyramid where row i is filled entirely with the i-th letter of the alphabet.

Input
5
Output
A B B B C C C C C D D D D D D D E E E E E E E E E

Java Program

Java
public class AlphabetPyramid { public static void main(String[] args) { int n = 5; for (int i = 1; i <= n; i++) { char letter = (char) ('A' + i - 1); StringBuilder row = new StringBuilder(); for (int s = 0; s < n - i; s++) row.append(" "); // leading spaces to center this row for (int j = 0; j < 2 * i - 1; j++) { if (j > 0) row.append(" "); row.append(letter); } System.out.println(row); } } }

Output

A B B B C C C C C D D D D D D D E E E E E E E E E

Core Logic

Picking one letter per row and repeating it enough times to fill that row's pyramid width, with the usual centering spaces in front, builds the whole shape.

How It Works
  1. 1(char) ('A' + i - 1) picks row i's single letter — 'A' for row 1, 'B' for row 2, and so on.
  2. 2n - i leading spaces are printed first, shrinking by one each row so the shape stays centered.
  3. 3The row's width is 2 * i - 1 — the same odd-number growth used by a star pyramid — but every position in it uses the same letter instead of a star.
  4. 4The letters are joined with single spaces the same way a star pyramid's stars are.
Row 3 uses the letter C, has n - 3 = 2 leading spaces, and repeats C across 2*3-1 = 5 positions: C C C C C.
💡

Key Point: Every row uses one fixed letter for its entire width — unlike Character Triangle, where each position gets a different letter, here the row index decides the letter and nothing else does.

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

Why: The total characters printed across every row — spaces and letters together — grows proportionally to n², with no storage beyond the loop counters and a per-row StringBuilder.

Key Concepts

nested for loopchar arithmeticpyramid centering

Approach 2: Java 8

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

Output

A B B B C C C C C D D D D D D D E E E E E E E E E

Core Logic

Building each row's leading spaces with String.repeat() and its repeated letter 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(char) ('A' + i - 1) picks that row's single letter, exactly the same char-arithmetic idea used to build a letter-by-letter triangle.
  3. 3" ".repeat(n - i) builds that row's leading spaces directly, without a separate loop.
  4. 4An inner IntStream.range(0, 2 * i - 1) maps every position to the row's letter and joins them with spaces.
For row 3, " ".repeat(2) gives two leading spaces, the letter is C, and the inner stream joins five copies of it into C C C C C.
💡

Key Point: This combines the two techniques already used separately elsewhere — char arithmetic picks the letter, String.repeat() handles the centering — showing they compose cleanly in a single stream pipeline.

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