C++ Reverse Alphabet Pyramid Pattern (Build to A)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A right-aligned reverse alphabet pyramid prints reverse letter suffixes (A, BA, CBA, …) under a fixed column width, with leading spaces so each row sits on the right.

Remember
Rule: for peak i = A..top,
      scan top..A — space while j > i, else print j

    A
   BA
  CBA
 DCBA
EDCBA     ← top = 'E'

Descending j gives reverse letter order; the j > i branch supplies the pads. An explicit pad-then-suffix style prints the same shape.

How to Solve It

Two equivalent styles — one fixed-width scan with a pad/letter branch, or separate pad and reverse-suffix loops.

MethodIdeaBest for
Scan + branchWalk top..A; j > i → space, else letterLearning, interviews, exams
Pad, then suffixpad = width - letters, then print i..AClearer demos once the shape clicks

Pseudocode

Pseudocode
top = 'E'
for i from 'A' to top:
    for j from top down to 'A':
        if j > i:
            print space
        else:
            print j
    print newline

Cheat sheet

GoalPattern
Pick the row peakfor (char i = 'A'; i <= top; i++)
Scan fixed widthfor (char j = top; j >= 'A'; j--)
Pad vs lettercout << (j > i ? ' ' : j);
End the rowcout << "\n";
Row widthn = top - 'A' + 1 columns

Printing Letters vs Starting a New Line

APIEffectUse for
cout << ' 'One padding space; stays on the lineColumns where j > i
cout << jOne letter; stays on the lineVisible reverse suffix
cout << "\n"Ends the current lineAfter the full-width scan

cout << endl also ends the line (and flushes); "\n" is enough for these demos.

Live Preview

Change the top letter and the right-aligned pyramid updates instantly — capped at H for readability.

Single uppercase letter from A to H. With E you get the classic sample.

Live result 5 rows · 5 cols
    A
   BA
  CBA
 DCBA
EDCBA

Worked Walkthrough — top = 'C'

Smaller board (3 columns) so pads and reverse suffixes are easy to check by hand.

Peak iSpaces (j > i)LettersPrinted row
'A'2AA
'B'1BABA
'C'0CBACBA

When i = 'B', column C is a space; then B and A print → BA.

C++ Programs

Three complete programs: fixed top letter, cin input, and an explicit pad-then-suffix rewrite. Use View Output to reveal sample results.

Example 1 — Fixed top at 'E'

Outer i is the row peak. Inner j sweeps E down to A and prints a space until it reaches i.

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

int main()
{
    for (char i = 'A'; i <= 'E'; i++)
    {
        for (char j = 'E'; j >= 'A'; j--)
        {
            if (j > i)
                cout << " ";
            else
                cout << j;
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer loop raises the peak. i runs from 'A' to 'E' — one row per visible suffix length.

2. Inner loop scans fixed width. j always walks from 'E' down to 'A' so every row has five columns.

3. Branch pad vs letter. While j > i, print a space; otherwise print j (reverse order).

4. Break the line. cout << "\n" after the scan — when i = 'C' you get two spaces then CBA.

Example 2 — User Input Version

Read the top letter with cin. Prefer validating a single A–Z character in real apps.

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

int main()
{
    char top;

    cout << "Enter the top letter (like E): ";
    cin >> top;

    for (char i = 'A'; i <= top; i++)
    {
        for (char j = top; j >= 'A'; j--)
            cout << (j > i ? ' ' : j);
        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and read. cin >> top sets both loop bounds.

2. Same pad rule. Only top changes; j > i still supplies leading spaces.

3. Ternary shortcut. cout << (j > i ? ' ' : j) is the same branch as Example 1 in one expression.

4. Safer input tip. Reject lowercase or non-letters:

Safer input
if (!(cin >> top) || top < 'A' || top > 'Z')
{
    cout << "Enter an uppercase letter A–Z.\n";
    return 1;
}

Example 3 — Pad spaces, then reverse suffix

Same pyramid with separate pad and letter loops — often easier to read.

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

int main()
{
    char top = 'E';
    int width = top - 'A' + 1;

    for (char i = 'A'; i <= top; i++)
    {
        int letters = i - 'A' + 1;
        int pad = width - letters;

        for (int s = 0; s < pad; s++)
            cout << ' ';

        for (char ch = i; ch >= 'A'; ch--)
            cout << ch;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Compute pads. Row peak i needs width - (i - 'A' + 1) leading spaces.

2. Print the reverse suffix. Walk ch from i down to 'A'.

3. Same visual result. Matches the scan style; use Examples 1–2 when you want every column visible in one loop.

Edge Cases & Pitfalls

Check these before calling the solution done.

Wrong compare

Misaligned pads

Use j > i (strict). j >= i eats the peak letter into a space.

Ascending j

Forward letters

If j runs A..top, you get forward suffixes (AB, ABC) — not this reverse pyramid.

\n early

Broken rows

Call cout << "\n" only after the full-width scan finishes.

top = 'A'

Single A

Output is just A — no pads on a one-column grid.

cin

Check failure

Use if (!(cin >> top)) and require a single A–Z letter.

Monospace

View in a fixed font

Right alignment only looks correct in a monospace console or <pre> block.

Time and Space Complexity

ProgramTimeExtra space
Scan style (Examples 1–2)O(n²)O(1)
Pad style (Example 3)O(n²)O(1)

For n = top - 'A' + 1 there are n rows and each row scans n columns — quadratic in n.

Key Takeaways

  • Rule: scan top..A; print space while j > i, else print j.
  • Fixed width: every row has n columns; pads push the reverse suffix right.
  • Descending scan: j-- is what makes BA, CBA, DCBA.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each peak i, scan top..A — space while j > i, else print j — then cout << "\n".

Frequently Asked Questions

j walks from E down to A. While j is above the current row peak i, print spaces; once j reaches i and below, print letters. That pushes the visible suffix (like CBA) to the right of a fixed-width line.
Because we print leading spaces for columns where j > i. That pushes the letters to the right and forms a right-aligned pyramid.
Descending j prints letters in reverse order (BA, CBA, DCBA). If j went upward, you would get AB, ABC, ABCD instead.
cout << ch stays on the same line for each cell. cout << "\n" ends the row after the fixed-width scan finishes.
Update the loop bounds so the outer loop runs up to H and the inner scan starts from H down to A.
Program 2 prints reverse prefixes left-aligned (E, ED, EDC…). This pattern prints reverse suffixes right-aligned with leading spaces (A, BA, CBA…).
O(n²) for n letters because there are n rows and each row scans n positions.
Use cin >> top, require A–Z, and reject failed input. Cap at Z if you only want alphabetic ranges.

Did you know?

Each line has fixed width (five columns for A…E). Scanning j from E down to A, letters higher than the row peak i turn into spaces, so the visible suffix (CBA, DCBA, …) sits on the right.

Next: Diamond with Stars

Build a diamond of letters framed with stars.

Program 21 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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