Right-Aligned Reverse Alphabet Pyramid in C++

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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):

Output
        E
      E D
    E D C
  E D C B
E D C B A
1

C++ Program (Reference Logic)

First print padding pairs, then print letters from E down to the current row letter.

C++
#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

1

Outer loop i (E → A)

Top row prints 1 letter (E), bottom row prints 5 letters (E D C B A).

Rows
2

Padding loop j = A .. i-1

Each iteration prints " ". As i decreases, padding shrinks (4 pairs down to 0).

Align
3

Letter loop j = E .. i

setw(2) gives each letter a 2-column cell to match the 2-space padding.

setw
=

Quick check

At i = C, you get 2 padding pairs and then E D C.

2

Variation — User Input

Compute end as 'A' + rows - 1, then run the same two-loop structure.

C++
#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 rows stays in 1..26

Avoid

  • Changing padding width without matching the setw() width
  • Running past Z without accounting for non-letter ASCII characters

Key Takeaways

1

Use a dedicated padding loop to right-align the triangle.

2

The letter loop prints end down to i each row (a reverse slice).

3

setw(2) + " " keeps a consistent grid in the console.

4

O(n²) time for n rows.

❓ Frequently Asked Questions

Padding shifts the visible letters to the right. Without it, the pyramid would be left-aligned.
Yes—character literals are clearer and behave the same (they compile to the same integer codes).
O(n²) for n rows.

Explore More C++ Alphabet Patterns!

Splitting padding and content into two loops is a clean way to build aligned console pyramids.

All Alphabet Patterns →
Did you know?

With 5 rows, you still print \(1 + 2 + 3 + 4 + 5 = 15\) letters in total—same count as program 22, just arranged differently.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful