Reverse Centered Alphabet Pyramid in C++

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 EC++ Program (Reference Logic)
Two phases share the same two inner loops; the second phase starts at B to skip the duplicate center.
#include <iostream>
using namespace std;
int main() {
int i, j;
int k = 69;
// Upper half (E -> A)
for (i = k; i >= 65; i--) {
for (j = k; j >= 65; j--) {
if (j > i) cout << char(j) << " ";
else cout << char(i) << " ";
}
for (j = 66; j <= k; j++) {
if (j > i) cout << char(j) << " ";
else cout << char(i) << " ";
}
cout << "\n";
}
// Lower half (B -> E)
for (i = 66; i <= k; i++) {
for (j = k; j >= 65; j--) {
if (j > i) cout << char(j) << " ";
else cout << char(i) << " ";
}
for (j = 66; j <= k; j++) {
if (j > i) cout << char(j) << " ";
else cout << char(i) << " ";
}
cout << "\n";
}
return 0;
}🧠 How It Works
Upper half: i from E down to A
Each row is a full-width “floor” line: on both inner sweeps, if j > i then cout << char(j) << ' ', else cout << char(i) << ' '. That is the same cell rule as program 28, repeated for each floor from E down to A.
Center row at i == 'A'
The middle text line is E D C B A B C D E. Here the floor is A, so the left and right segments meet with a single A between descending and ascending halves.
Lower half: i from B back to E
A second outer loop increases i again with the same inner structure, rebuilding the upper rows in reverse so the pyramid completes without duplicating the widest border row.
Nine symbol rows for five letters
Each line prints nine letter tokens plus separating spaces from cout << ' '. You get 2 × 5 - 1 = 9 rows: five steps down to the A floor, then four steps back up to the outer E frame.
Symmetry
Adjacent rows around the center differ only in the middle run (A vs B here). Nested sweeps per row keep the complexity at O(n²) for span n.
Variation — User Input
Pick rows (alphabet span). The peak letter is endChar = 'A' + rows - 1.
#include <iostream>
using namespace std;
static void printRow(char i, char startChar, char endChar) {
for (char j = endChar; j >= startChar; --j) {
cout << (j > i ? j : i) << ' ';
}
for (char j = char(startChar + 1); j <= endChar; ++j) {
cout << (j > i ? j : i) << ' ';
}
cout << "\n";
}
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
char startChar = 'A';
char endChar = char('A' + rows - 1);
for (char i = endChar; i >= startChar; --i) {
printRow(i, startChar, endChar);
}
for (char i = char(startChar + 1); i <= endChar; ++i) {
printRow(i, startChar, endChar);
}
return 0;
}💡 Tips for Enhancement
Try These
- Extract the repeated row-printing logic into a function (as in the variation)
- Remove the trailing space for a compact output block
- Keep
rows <= 26to stay in A..Z
Avoid
- Starting the second phase at
A(duplicates the center row) - Changing only one half’s loop bounds (breaks symmetry)
Key Takeaways
Two phases (down then up) move the floor letter from the outer edge to A and back, keeping the same row rule.
Lower half starts at B to avoid duplicating the center.
Total rows for n letters is 2n - 1.
O(n²) output for span n.
❓ Frequently Asked Questions
i changes at the center. Splitting makes it easy to skip the duplicate center row.(2n - 1) rows and (2n - 1) cells per row, so total is (2n - 1)². For n = 5, that is 9² = 81 cells (plus spaces/newlines).Explore More C++ Alphabet Patterns!
Try building a hollow variant by printing the shell letters only when you are on the border.
For n letters you print \((2n - 1)\) rows and \((2n - 1)\) letter cells per row, so \((2n - 1)^2\) cells in total (plus spaces and newlines).
12 people found this page helpful
