Reverse Centered Alphabet Pyramid in Java

What You’ll Learn
This reverse centered alphabet pyramid reuses the row logic from program 28: print downward from E to A, then upward from B to E so the center row is not repeated.
Each cell still uses the same rule: if j > i, print j, else print i.
⭐ Pattern Output
Nine rows for A…E (space after each letter):
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E EJava Program (Fixed A–E)
Two phases share the same two inner loops; the second phase starts at B to skip the duplicate center.
public class Main {
public static void main(String[] args) {
int i, j;
int k = 69; // 'E'
// Upper half (E -> A)
for (i = k; i >= 65; i--) {
for (j = k; j >= 65; j--) {
if (j > i) System.out.print((char) j + \" \");
else System.out.print((char) i + \" \");
}
for (j = 66; j <= k; j++) {
if (j > i) System.out.print((char) j + \" \");
else System.out.print((char) i + \" \");
}
System.out.println();
}
// Lower half (B -> E)
for (i = 66; i <= k; i++) {
for (j = k; j >= 65; j--) {
if (j > i) System.out.print((char) j + \" \");
else System.out.print((char) i + \" \");
}
for (j = 66; j <= k; j++) {
if (j > i) System.out.print((char) j + \" \");
else System.out.print((char) i + \" \");
}
System.out.println();
}
}
}🧠 How It Works
Upper half then lower half
First outer loop prints E down to A. Second outer loop prints B back up to E.
Two symmetric scans per row
Each row prints a left half (E…A) then a right half (B…E) to stay symmetric.
One rule builds the shells
If j > i print j else print i. That keeps higher letters on the outside.
Why start the lower half at B?
The center row with A is already printed in the upper half. Starting at B avoids duplicating it.
9-by-9 output grid
For 5 letters (A…E), total rows are 2n - 1 = 9, each row nine letters wide.
Variation — User Input
Generalize by choosing the top letter with rows (clamped 1–26). Upper half: top down to A. Lower half: B up to top.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
rows = Math.max(1, Math.min(rows, 26));
char top = (char) ('A' + rows - 1);
for (char i = top; i >= 'A'; i--) {
for (char j = top; j >= 'A'; j--) System.out.print((j > i ? j : i) + " ");
for (char j = 'B'; j <= top; j++) System.out.print((j > i ? j : i) + " ");
System.out.println();
}
for (char i = 'B'; i <= top; i++) {
for (char j = top; j >= 'A'; j--) System.out.print((j > i ? j : i) + " ");
for (char j = 'B'; j <= top; j++) System.out.print((j > i ? j : i) + " ");
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Remove the trailing space after the last character on each row
- Try a numeric version using the same shells logic
- Switch to lowercase by starting from
'a'
Avoid
- Starting the lower half from
A(duplicates the center row) - Letting
rowsexceed 26 without handling non-letter output
Key Takeaways
Two phases move the floor letter from the outer edge to A and back, using the same row rule.
Lower half starts at B to avoid duplicating the center row.
Each cell uses the same rule: print j if j > i, else print i.
O(n²) total work for n letters.
❓ Frequently Asked Questions
A-centered row. Starting at B prevents a duplicate center line.A to the top letter, total rows are 2n - 1.More Java alphabet patterns
Once you can build one phase cleanly, repeating it in reverse is an easy way to close reverse centered pyramids and other symmetric grids.
Many symmetric block patterns are an upper part plus the same logic repeated in reverse, with the middle row printed only once.
12 people found this page helpful
