A symmetric alphabet pyramid prints centered palindrome rows: each line climbs from A to a peak letter, then mirrors back to A without repeating the center.
Remember
Rule: spaces + A..i + (i−1)..A
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA ← top = 'E'
Three nested loops do the work: pad with spaces, print the ascending half, then mirror downward from i − 1. Compare with Program 18 (another palindrome pyramid) and Program 31 (hollow V).
Approach
How to Solve It
Two ways to emit the same pyramid — classic bridge variable --n, or an explicit descending loop.
Method
Idea
Best for
Bridge n = k − 1
After A..i, print --n for i steps
Matching classic textbook / exam code
Explicit mirror
Loop p from i − 1 down to 0
Clearer demos once the skip-center rule clicks
Pseudocode
Pseudocode
end = top - 'A'
for i from 0 to end:
print (end - i) spaces
for k from 0 to i:
print alpha[k]
for p from i - 1 down to 0:
print alpha[p]
print newline
Cheat sheet
Goal
Pattern
Top index
int end = top - 'A'; (4 for E)
Rows
for (int i = 0; i <= end; i++)
Leading spaces
for (int j = end; j > i; j--) cout << ' ';
Ascending half
for (int k = 0; k <= i; k++) cout << alpha[k];
Bridge mirror
int n = k - 1; for (int m = 0; m < i; m++) cout << alpha[--n];
Explicit mirror
for (int p = i - 1; p >= 0; p--) cout << alpha[p];
Letter count
(i + 1) + i = 2i + 1
Printing Letters vs Starting a New Line
API
Effect
Use for
cout << ch
Stays on the same line
Each space or letter
cout << "\n"
Ends the current line
After spaces + ascend + mirror finish a row
Print cells without a newline, then end the row once.
Try it
Live Preview
Change the top letter and the pyramid updates instantly — each row stays a centered palindrome of odd length.
One letter from A to F. Tap a chip or type a letter — use a monospace view for centering.
Live resultTop E · 5 rows · base 9 letters
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
Trace
Worked Walkthrough — Top = E (end = 4)
Trace padding, ascending half, and mirrored descending half for each row.
i
Spaces
Ascending
Mirror
Printed row
0
4
A
(none)
A
1
3
AB
A
ABA
2
2
ABC
BA
ABCBA
3
1
ABCD
CBA
ABCDCBA
4
0
ABCDE
DCBA
ABCDEDCBA
After ascending, k = i + 1, so n = k − 1 = i. The first --n lands on i − 1 — that is why the peak is not duplicated.
Code
C++ Programs
Three complete programs: fixed A–E, top-letter input, and an explicit-mirror rewrite. Use View Output to reveal sample results.
Example 1 — Fixed A–E
Hard-coded height with the classic bridge variable n = k − 1 for the descending half.
C++
#include <iostream>
using namespace std;
int main()
{
int i, j, k, m, n;
const char* alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (i = 0; i <= 4; i++)
{
for (j = 4; j > i; j--)
cout << " ";
for (k = 0; k <= i; k++)
cout << alpha[k];
n = k - 1;
for (m = 0; m < i; m++)
cout << alpha[--n];
cout << "\n";
}
return 0;
}
Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
How It Works
1. Outer loop picks the row.i runs from 0 to 4 (letters A…E).
2. Pad for centering. Print 4 − i spaces so the pyramid lines up under the widest row.
3. Ascending half. Loop k = 0..i and print alpha[k] (for i = 2 that is ABC).
4. Mirror without the peak. Set n = k − 1, then print --n for i steps (BA when i = 2) → ABCBA.
Example 2 — Top Letter Input
The loops adapt to the new size automatically. Prefer validating a single A–Z character in real apps.
C++
#include <iostream>
using namespace std;
int main()
{
char top;
cout << "Enter top letter (like E): ";
cin >> top;
int end = top - 'A';
const char* alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i <= end; i++)
{
for (int j = end; j > i; j--)
cout << " ";
int k;
for (k = 0; k <= i; k++)
cout << alpha[k];
int n = k - 1;
for (int m = 0; m < i; m++)
cout << alpha[--n];
cout << "\n";
}
return 0;
}
Output (when user enters C)
Enter top letter (like E): C
A
ABA
ABCBA
How It Works
1. Scale with end.end = top - 'A' drives padding, ascending, and mirror together.
2. Same three-stage core. Only the source of end changes — the print logic matches Example 1.
3. Safer input tip. Prefer:
Safer input
#include <cctype>
if (!(cin >> top))
{
cout << "Enter one letter from A to Z.\n";
return 1;
}
top = (char)toupper((unsigned char)top);
if (top < 'A' || top > 'Z')
{
cout << "Enter one letter from A to Z.\n";
return 1;
}
Example 3 — Explicit Mirror Loop
Often clearer: after printing A..i, loop p from i − 1 down to 0.
C++
#include <iostream>
using namespace std;
int main()
{
int end = 4;
const char* alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i <= end; i++)
{
for (int j = end; j > i; j--)
cout << " ";
for (int k = 0; k <= i; k++)
cout << alpha[k];
for (int p = i - 1; p >= 0; p--)
cout << alpha[p];
cout << "\n";
}
return 0;
}
Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
How It Works
1. Skip the peak on purpose.p starts at i − 1, so the center letter from the ascending half is not printed again.
2. Same shape as Example 1. Output matches the bridge-variable version exactly — pick whichever style you find easier to explain.
Edge Cases & Pitfalls
Check these before calling the solution done.
Mirror from i
Doubled center
If the descending loop starts at i instead of i − 1, you get ABCCBA-style rows. Always skip the peak.
No spaces
Left-aligned stack
Without leading spaces the letters still form palindromes, but the shape is no longer a centered pyramid.
\n early
Column of cells
If cout << "\n" is inside any inner loop, each character lands on its own line. End the row only after all three stages.
top = A
Single A
When end = 0, you get one row with no spaces and no mirror. A good sanity check.
Wrong pad
Off-center rows
Pad count must be end − i (loop j from end while j > i). Using a fixed space count skews every row.
Bad cin
Validate one letter
Check cin >> top, optionally uppercase, and require A–Z before the outer loop.
Analysis
Time and Space Complexity
Program
Time
Extra space
Bridge variable (Examples 1–2)
O(n²)
O(1) (plus the fixed alphabet table)
Explicit mirror (Example 3)
O(n²)
O(1)
For top letter with index n there are n + 1 rows and row i prints (n − i) spaces plus 2i + 1 letters — still quadratic in n.
Remember
Key Takeaways
Rule: spaces + A..i + (i−1)..A on every row.
Skip the peak: mirror starts at i − 1 (or use --n after n = k − 1).
Break the row: print cells with cout; call cout << "\n" only after all three stages.
Complexity:O(n²) time; O(1) extra space.
One line: for each row, pad, climb A..i, then mirror (i−1)..A without repeating the center.
Frequently Asked Questions
It prints A..i ascending, then prints (i-1)..A descending. That mirrors the left half without repeating the center letter.
After the ascending loop, k is one past the peak. Setting n = k-1 makes n equal the peak, and --n starts the descending half from the previous letter.
The padding shifts each row so the pyramid is centered under the widest row.
O(n²) for n rows because each row prints O(n) spaces and letters.
After printing A..i, the mirror starts from i-1 down to A, so the peak letter appears once.
Use cin >> top, require A–Z (or a–z with toupper), and reject failed input. Cap at Z if you only want alphabetic ranges.
Ascending prints i+1 letters and descending prints i letters, so the row has 2i+1 letters before counting spaces.
Program 18 is another palindromic alphabet pyramid. This page follows the classic bridge-variable style with n = k-1 and --n, plus an explicit mirror rewrite.
🤔
Did you know?
Each row has three stages: leading spaces for centering, letters A..i ascending, then (i-1)..A descending. After the ascending loop, n = k - 1 and --n start the mirror without duplicating the center letter.