Java Palindromic Alphabet Pyramid Pattern (Center A)
Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A palindromic alphabet pyramid prints mirrored letter rows that grow around a single center A: each line reads the same forward and backward.
Remember
Rule: for peak i from 'A' to top,
print i..B (descending), then A..i (ascending)
A
BAB
CBABC
DCBABCD
EDCBABCDE ← 5 rows (top = 'E')
The descending wing stops before A so the center letter is not printed twice. Compare with Program 18 (different wing order) and add pads (or see Program 16) if you want the shape centered.
Approach
How to Solve It
Two ways to emit the same palindrome — classic char loops, then optionally space each letter for a wider look.
Method
Idea
Best for
Descend + ascend
Left wing i..B; right wing A..i
Learning, interviews, exams
Spaced letters
Same wings; print ch + " "
Clearer demos in dense terminals
Pseudocode
Pseudocode
for i from 'A' to top:
for j from i down to 'B':
print j
for j from 'A' to i:
print j
print newline
Cheat sheet
Goal
Pattern
Pick the peak
for (char i = 'A'; i <= 'E'; i++)
Left wing
for (char j = i; j > 'A'; j--) System.out.print(j);
Center + right
for (char j = 'A'; j <= i; j++) System.out.print(j);
End the row
System.out.println();
Row length
2 * (i - 'A') + 1 (odd widths)
Peak from rows
char peak = (char)('A' + r);
Avoid double A
Use j > 'A', not j >= 'A'
Printing Letters vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each letter on both wings
System.out.println
Ends the current line
After both inner loops
Print letters without a newline, then end the row once.
Try it
Live Preview
Change the height and the palindrome updates instantly — including the letter total (n²).
Whole numbers from 1 to 10. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 25 letters
A
BAB
CBABC
DCBABCD
EDCBABCDE
Trace
Worked Walkthrough — A–E
Trace each row’s left wing, center + right wing, and full line.
i
Left (i..B)
Right (A..i)
Printed row
A
(empty)
A
A
B
B
AB
BAB
C
CB
ABC
CBABC
D
DCB
ABCD
DCBABCD
E
EDCB
ABCDE
EDCBABCDE
Lengths: 1 + 3 + 5 + 7 + 9 = 25 = 5². That odd-number sum is why time is O(n²).
Code
Java Programs
Three complete programs: fixed A–E, Scanner row-count input, and a spaced-letter variant. Use View Output to reveal sample results.
Example 1 — Fixed A–E
Two loops per row: descending (i..B), then ascending (A..i). The descending loop stops before A.
Java
class PalindromicPyramid {
public static void main(String[] args) {
for (char i = 'A'; i <= 'E'; i++) {
for (char j = i; j > 'A'; j--)
System.out.print(j);
for (char j = 'A'; j <= i; j++)
System.out.print(j);
System.out.println();
}
}
}
Output
A
BAB
CBABC
DCBABCD
EDCBABCDE
How It Works
1. Outer loop picks the peak.i runs from 'A' to 'E' — one row per peak letter.
2. Left wing descends. Print j from i down while j > 'A' (stops at B).
3. Center + right ascend. Print j from 'A' through i — that supplies the single center A.
4. Break the line.System.out.println() after both wings starts the next row.
When i = 'C', the left wing prints CB and the right wing prints ABC → CBABC.
Example 2 — Row Count Input
Cap at 26 for A–Z. Prefer hasNextInt() in real apps.
Java
import java.util.Scanner;
class PalindromicPyramidInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows (1..26): ");
int n = sc.nextInt();
if (n < 1) return;
if (n > 26) n = 26;
for (int r = 0; r < n; r++) {
char peak = (char) ('A' + r);
for (char ch = peak; ch > 'A'; ch--)
System.out.print(ch);
for (char ch = 'A'; ch <= peak; ch++)
System.out.print(ch);
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter number of rows (1..26): 4
A
BAB
CBABC
DCBABCD
How It Works
1. Prompt and clamp. Read a row count and keep it in 1–26 so peaks stay in A–Z.
2. Map index to peak. Row r uses peak = (char)('A' + r).
3. Same wing core. Descend/ascend rules match Example 1 — only the number of peaks changes.
4. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a whole number from 1 to 26.");
return;
}
int n = sc.nextInt();
if (n < 1 || n > 26) {
System.out.println("Enter a whole number from 1 to 26.");
return;
}
Example 3 — Spaced Letters
Same palindrome with a space after each letter for a wider look.
Java
class PalindromicPyramidSpaced {
public static void main(String[] args) {
for (char i = 'A'; i <= 'E'; i++) {
for (char j = i; j > 'A'; j--)
System.out.print(j + " ");
for (char j = 'A'; j <= i; j++)
System.out.print(j + " ");
System.out.println();
}
}
}
Output
A
B A B
C B A B C
D C B A B C D
E D C B A B C D E
How It Works
1. Same wings. Still descend i..B, then ascend A..i.
2. Format only.j + " " adds a trailing space after each letter.
3. Optional polish. Trim the final space per row if you need a clean line end.
Edge Cases & Pitfalls
Check these before calling the solution done.
j >= 'A'
Duplicate center A
If the descending loop uses j >= 'A', you get BAAB or CBABBC. Keep j > 'A'.
Wing order
Wrong palindrome style
Ascending first then descending gives Program 18-style rows (e.g. ABCBA) instead of CBABC.
println inside
Column of letters
If println is inside either wing loop, each letter lands on its own line. Use print for letters; println only after both loops.
n = 1
Single A
Left wing never runs; output is just A. A good sanity check.
n > 26
Past Z
Clamp or reject so peaks stay in A–Z.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require 1–26.
Analysis
Time and Space Complexity
Program
Time
Extra space
Descend + ascend (Examples 1–2)
O(n²)
O(1)
Spaced letters (Example 3)
O(n²)
O(1)
Total letters = 1 + 3 + … + (2n - 1) = n². Spaced output prints the same letters plus spaces — still quadratic in n.
Remember
Key Takeaways
Rule: for peak i, print i..B then A..i.
Single center A: stop the descending loop at j > 'A'.
Break the row: call println only after both wings.
Complexity:O(n²) time from n² letters; O(1) extra space.
One line: for each peak i, print descending i..B, then ascending A..i, then println().
Frequently Asked Questions
The first loop prints descending letters from the row peak down to B. The second prints ascending from A through i. Together they mirror around a single A.
The forward loop already prints A. Stopping the descending loop before A avoids duplicating the center character.
The descending loop stops at j > 'A', and the ascending loop starts at 'A'. That prevents printing A twice at the join.
For row letter i, length is 2*(i-'A')+1, so rows grow as 1, 3, 5, 7, 9 for A..E.
Change the outer loop upper bound from 'E' to your target letter (like 'H'), or use the row-count Scanner variant.
Program 18 typically builds A..peak then peak-1..A. This pattern descends to B first, then ascends A..i, still mirroring around a single A.
O(n²) for n rows because total printed characters are 1+3+...+(2n-1) = n².
No — the classic version is left-aligned with no leading spaces. Add pad spaces (or see Program 16) if you want a centered pyramid.
🤔
Did you know?
Outer i runs A.. E. First inner loop prints the left wing (i down to B) by stopping at j > 'A'. Second inner loop prints A.. i. Because the reverse loop stops before A, the center A is not duplicated.