C++ 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 then a space
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--) cout << j;
Center + right
for (char j = 'A'; j <= i; j++) cout << j;
End the row
cout << "\n";
Row length
2 * (i - 'A') + 1 (odd widths)
Peak from rows
char peak = (char)('A' + r); (0-based r)
Avoid double A
Use j > 'A', not j >= 'A'
Printing Letters vs Starting a New Line
API
Effect
Use for
cout << j
Stays on the same line
Each letter on both wings
cout << "\n"
Ends the current line
After both inner loops
Print characters 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
C++ Programs
Three complete programs: fixed A–E, row-count input with cin, 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.
C++
#include <iostream>
using namespace std;
int main() {
char i, j;
for (i = 'A'; i <= 'E'; i++) {
for (j = i; j > 'A'; j--)
cout << j;
for (j = 'A'; j <= i; j++)
cout << j;
cout << "\n";
}
return 0;
}
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.cout << "\n" 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. Check cin in real apps.
C++
#include <iostream>
using namespace std;
int main() {
int n, r;
char peak, ch;
cout << "Enter number of rows (1..26): ";
cin >> n;
if (n < 1) return 0;
if (n > 26) n = 26;
for (r = 0; r < n; r++) {
peak = (char)('A' + r);
for (ch = peak; ch > 'A'; ch--)
cout << ch;
for (ch = 'A'; ch <= peak; ch++)
cout << ch;
cout << "\n";
}
return 0;
}
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 (!(cin >> n) || n < 1 || n > 26) {
cout << "Enter a whole number from 1 to 26.\n";
return 1;
}
Example 3 — Spaced Letters
Same palindrome with a space after each letter for a wider look.
C++
#include <iostream>
using namespace std;
int main() {
char i, j;
for (i = 'A'; i <= 'E'; i++) {
for (j = i; j > 'A'; j--)
cout << j << ' ';
for (j = 'A'; j <= i; j++)
cout << j << ' ';
cout << "\n";
}
return 0;
}
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.cout << 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.
cout "\n" inside
Column of letters
If cout << "\n" is inside either wing loop, each letter lands on its own line. Print letters with cout; end the row 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.
cin fail
Check the read
cin >> n can fail on empty or non-numeric text. Prefer if (!(cin >> n)) 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 cout << "\n" 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 cout << "\n".
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 input 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.
cout << ch stays on the same line. cout << "\n" ends the current line. Letters use cout; the row break uses cout << "\n" after both wings.
O(n²) for n rows because total printed characters are 1+3+...+(2n-1) = n².
🤔
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.