Right-Aligned Reverse Alphabet Pyramid in C++

What You’ll Learn
This is a companion to program 22: it uses the same fixed-width grid (two spaces for padding + setw(2) for letters), but prints a reverse alphabet slice on each row.
Use a monospace font in your terminal so columns stay even.
⭐ Pattern Output
Output for 5 rows (E down to A):
E
E D
E D C
E D C B
E D C B AC++ Program (Reference Logic)
First print padding pairs, then print letters from E down to the current row letter.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int i, j;
for (i = 69; i >= 65; i--) {
for (j = 65; j < i; j++)
cout << " ";
for (j = 69; j >= i; j--)
cout << setw(2) << char(j);
cout << "\n";
}
return 0;
}🧠 How It Works
Outer loop i (E → A)
Top row prints 1 letter (E), bottom row prints 5 letters (E D C B A).
Padding loop j = A .. i-1
Each iteration prints " ". As i decreases, padding shrinks (4 pairs down to 0).
Letter loop j = E .. i
setw(2) gives each letter a 2-column cell to match the 2-space padding.
Quick check
At i = C, you get 2 padding pairs and then E D C.
Variation — User Input
Compute end as 'A' + rows - 1, then run the same two-loop structure.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
const int start = 'A';
int end = start + rows - 1;
for (int i = end; i >= start; --i) {
for (int j = start; j < i; ++j) {
cout << " ";
}
for (int j = end; j >= i; --j) {
cout << setw(2) << char(j);
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Switch to character loops (
for (char i = end; i >= 'A'; --i)) - Use lowercase by starting at
'e'and ending at'a' - Add input validation so
rowsstays in 1..26
Avoid
- Changing padding width without matching the
setw()width - Running past
Zwithout accounting for non-letter ASCII characters
Key Takeaways
Use a dedicated padding loop to right-align the triangle.
The letter loop prints end down to i each row (a reverse slice).
setw(2) + " " keeps a consistent grid in the console.
O(n²) time for n rows.
❓ Frequently Asked Questions
Explore More C++ Alphabet Patterns!
Splitting padding and content into two loops is a clean way to build aligned console pyramids.
With 5 rows, you still print \(1 + 2 + 3 + 4 + 5 = 15\) letters in total—same count as program 22, just arranged differently.
12 people found this page helpful
