Symmetric Alphabet Pyramid in C++

What You’ll Learn
This pattern prints centered palindromic rows: A, ABA, ABCBA, up to ABCDEDCBA.
We pad with spaces, print the left half ascending, then print the right half descending without repeating the middle letter.
⭐ Pattern Output
Output for 5 rows (A..E):
A
ABA
ABCBA
ABCDCBA
ABCDEDCBAC++ Program (Reference Logic)
Uses an n = k - 1 bridge variable like the reference to print the mirrored tail.
#include <iostream>
using namespace std;
int main() {
int i, j, k, m, n;
for (i = 65; i <= 69; i++) {
for (j = 69; j >= i; j--)
cout << " ";
for (k = 65; k <= i; k++)
cout << char(k);
n = k - 1;
for (m = 65; m < i; m++)
cout << char(--n);
cout << "\n";
}
return 0;
}🧠 How It Works
Leading spaces
j runs from 69 down to i; each step prints " " with cout so shorter rows shift right and the pyramid aligns under the widest line.
Ascending half
k from 65 through i prints char(k), building the left side including the peak letter for that row.
n = k - 1
When the second loop exits, k == i + 1. Setting n = k - 1 puts n back on the peak so the first --n yields i - 1 for the mirror.
Mirror with --n
m runs i - 65 times (zero when i == 65). Each cout << char(--n) prints the next letter down without repeating the center character.
Palindrome row
The peak prints only in the ascending loop; the descending loop walks from i - 1 down to A. Five outer rows with proportional inner work → O(n²) for n letters.
Variation — User Input (char literals)
Same pattern, but written with character variables ('A'.. endChar).
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
char startChar = 'A';
char endChar = char('A' + rows - 1);
for (char i = startChar; i <= endChar; ++i) {
for (char j = endChar; j >= i; --j) cout << ' ';
for (char k = startChar; k <= i; ++k) cout << k;
for (char k = char(i - 1); k >= startChar; --k) cout << k;
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Print a space between letters for a wider pyramid
- Use lowercase by starting at
'a' - Limit
rowsso output stays within letters A..Z
Avoid
- Printing the peak letter again on the way down (duplicates the center)
- Mixing different padding widths without checking alignment
Key Takeaways
Build rows as palindromes: ascending then descending.
Don’t duplicate the middle letter: start descending from i - 1.
Leading spaces keep the pyramid centered.
O(n²) output for n rows.
❓ Frequently Asked Questions
i - 1 (or uses --n), so the peak is printed only once.Explore More C++ Alphabet Patterns!
Once you can build one palindromic row, you can generate diamonds and more complex symmetric patterns.
Row lengths here are odd: 1, 3, 5, 7, 9… because each row is a palindrome with one center character.
12 people found this page helpful
