Java ProgramsPatternsPrint Inverted Alphabet Pyramid

Print Inverted Alphabet Pyramid in Java

intermediate·  Patterns  ·  Character Patterns

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

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

Java Program

Java
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

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

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.

How It Works
  1. 1The outer loop runs i from n down to 1, so the widest row is produced first.
  2. 2(char) ('A' + i - 1) still picks row i's letter, and 2 * i - 1 is still that row's width — identical to Alphabet Pyramid.
  3. 3n - i leading spaces still center each row, now growing instead of shrinking as i counts down.
  4. 4Only the loop's direction changed — every per-row calculation is untouched.
The first row printed is 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.

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

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

nested for loopchar arithmeticpyramid centering

Approach 2: Java 8

Java
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

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

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.

How It Works
  1. 1IntStream.rangeClosed(1, n).map(k -> n + 1 - k) produces the row indices n down to 1, the reverse of the upright pyramid's ascending stream.
  2. 2(char) ('A' + i - 1) still picks row i's letter, and " ".repeat(n - i) still centers it — the identical per-row rules from Alphabet Pyramid.
  3. 3An inner IntStream.range(0, 2 * i - 1) maps every position to the row's letter and joins them with spaces.
  4. 4forEach(System.out::println) prints each completed row as it's produced.
The first row index produced is 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.

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

Why: The nested streams still produce one letter per grid position across all rows, proportional to n², regardless of visiting order.

Key Concepts

StreamIntStream.rangeClosed()Collectors.joining()

Related Programs