Print Alphabet Pyramid in Java
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.
Java Program
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
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.
- 1
(char) ('A' + i - 1)picks rowi's single letter — 'A' for row 1, 'B' for row 2, and so on. - 2
n - ileading spaces are printed first, shrinking by one each row so the shape stays centered. - 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. - 4The letters are joined with single spaces the same way a star pyramid's stars are.
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.
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
Approach 2: Java 8
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
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.
- 1
IntStream.rangeClosed(1, n)produces one stream element per row numberi. - 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
" ".repeat(n - i)builds that row's leading spaces directly, without a separate loop. - 4An inner
IntStream.range(0, 2 * i - 1)maps every position to the row's letter and joins them with spaces.
" ".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.
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.