Right-Aligned Reverse Pyramid in C++

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

What You’ll Learn

Each row is a reverse suffix A, BA, CBA, … padded on the left so letters line up on the right in a fixed-width column.

Same j > i idea as the left half of program 19, without the second mirror loop.

⭐ Pattern Output

Monospace (leading spaces matter):

Output
    A
   BA
  CBA
 DCBA
EDCBA
1

Complete C++ Program (Reference Logic)

Outer i is the row peak; inner j sweeps the full alphabet slice once per row from E down to A.

C++
#include <iostream>
using namespace std;

int main() {
    int i, j;
    for (i = 65; i <= 69; i++) {
        for (j = 69; j >= 65; j--) {
            if (j > i) cout << " ";
            else cout << char(j);
        }
        cout << "\n";
    }
    return 0;
}

🧠 How It Works

1

Outer i

How far the reverse run extends this row: down from i to A.

Peak
2

Inner j scan

j always runs from endChar down to startChar, so each line has a fixed width.

Width
3

if (j > i)

Print a space for letters that belong to higher rows only.

Pad
4

Else branch

Print j, creating a contiguous reverse suffix like CBA.

Suffix
=

Leading spaces

Row peak i has endChar - i spaces before the first printed letter.

2

Variation — User Input

endChar replaces 'E' in the inner loop bound.

C++
#include <iostream>
using namespace std;

int main() {
    int rows;
    int i, j;
    char startChar, endChar;

    cout << "Enter the number of rows: ";
    cin >> rows;

    startChar = 'A';
    endChar = (char)('A' + rows - 1);

    for (i = startChar; i <= endChar; ++i) {
        for (j = endChar; j >= startChar; --j) {
            if (j > i) cout << " ";
            else cout << (char) j;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Separate loop: print endChar - i spaces, then print j from i down to startChar
  • Flip to a forward triangle by scanning j upward
  • Use a monospace font so columns line up

Avoid

  • j >= i for the space test — that turns the peak column into a space and drops the widest letter on the row
  • Mixing different endChar in outer vs inner loops

Key Takeaways

1

Descending j over a fixed range yields reverse order plus padding.

2

j > i prints spaces; j <= i prints letters.

3

Leading space count is endChar - i.

4

O(n²) for n rows with width n.

❓ Frequently Asked Questions

Program 11 repeats one letter per row. Here each row lists distinct descending letters from i to A, only shifted right.
The bottom row prints the full slice endChar through A; shorter rows pad on the left inside that same width.
That would treat the peak column as space when j == i and skip printing the widest letter. Stick to j > i.
O(n²) for n rows.

Explore More C++ Alphabet Patterns!

Right alignment is “print the padding, then the payload” inside a fixed column count.

All Alphabet Patterns →
Did you know?

This is the same geometry as printing a left-justified reverse triangle, then shifting every line right by endChar - i spaces.

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