Print Inverted Alphabet Pyramid in Java
Problem
Flipping a pyramid upside down just means running its row index backward — the same per-row letter and centering rules from Alphabet Pyramid still apply, only the order changes.
Given a number of rows, print an inverted centered pyramid where the widest row comes first and each row is filled with a single letter, ending at 'A'.
Java Program
public class InvertedAlphabetPyramid {
public static void main(String[] args) {
int n = 5;
for (int i = n; i >= 1; i--) {
char letter = (char) ('A' + i - 1); // row i's own letter
StringBuilder row = new StringBuilder();
for (int s = 0; s < n - i; s++) row.append(" ");
for (int j = 0; j < 2 * i - 1; j++) {
if (j > 0) row.append(" ");
row.append(letter);
}
System.out.println(row);
}
}
}Output
Core Logic
Running the same per-row letter-and-width rule from widest row to narrowest, instead of the other way around, turns the pyramid upside down without changing how any individual row is built.
- 1The outer loop runs
ifromndown to1, so the widest row is produced first. - 2
(char) ('A' + i - 1)still picks rowi's letter, and2 * i - 1is still that row's width — identical to Alphabet Pyramid. - 3
n - ileading spaces still center each row, now growing instead of shrinking asicounts down. - 4Only the loop's direction changed — every per-row calculation is untouched.
i = 5, giving the letter E repeated 9 times with no leading spaces; the last row is i = 1, giving a single A with the most leading spaces.Key Point: Reusing the exact same per-row formulas and just reversing the loop's direction is simpler than deriving a whole new set of rules for the upside-down shape.
Why: The total characters printed across every row grows proportionally to n², the same cost as the upright pyramid — only the print order changes.
Key Concepts
Approach 2: Java 8
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class InvertedAlphabetPyramidStream {
public static void main(String[] args) {
int n = 5;
IntStream.rangeClosed(1, n)
.map(k -> n + 1 - k) // widest row first
.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
Reusing the alphabet pyramid's exact per-row formula, but feeding it row indices in descending order instead of ascending, flips the shape the same way reversing the loop does.
- 1
IntStream.rangeClosed(1, n).map(k -> n + 1 - k)produces the row indicesndown to1, the reverse of the upright pyramid's ascending stream. - 2
(char) ('A' + i - 1)still picks rowi's letter, and" ".repeat(n - i)still centers it — the identical per-row rules from Alphabet Pyramid. - 3An inner
IntStream.range(0, 2 * i - 1)maps every position to the row's letter and joins them with spaces. - 4
forEach(System.out::println)prints each completed row as it's produced.
5, giving the letter E repeated across the widest row with no leading spaces; the last is 1, giving a single A.Key Point: Only the row-index stream's direction changes here — the exact same per-row mapping function from Alphabet Pyramid is reused unmodified, the same relationship the loop version has to its own upright counterpart.
Why: The nested streams still produce one letter per grid position across all rows, proportional to n², regardless of visiting order.