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.
Approach
How to Solve It
Two equivalent styles — one fixed-width scan with a pad/letter branch, or separate pad and reverse-suffix loops.
Method
Idea
Best for
Scan + branch
Walk top..A; j > i → space, else letter
Learning, interviews, exams
Pad, then suffix
pad = width - letters, then print i..A
Clearer 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
Goal
Pattern
Pick the row peak
for (char i = 'A'; i <= top; i++)
Scan fixed width
for (char j = top; j >= 'A'; j--)
Pad vs letter
cout << (j > i ? ' ' : j);
End the row
cout << "\n";
Row width
n = top - 'A' + 1 columns
Printing Letters vs Starting a New Line
API
Effect
Use for
cout << ' '
One padding space; stays on the line
Columns where j > i
cout << j
One letter; stays on the line
Visible reverse suffix
cout << "\n"
Ends the current line
After the full-width scan
cout << endl also ends the line (and flushes); "\n" is enough for these demos.
Try it
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 result5 rows · 5 cols
A
BA
CBA
DCBA
EDCBA
Trace
Worked Walkthrough — top = 'C'
Smaller board (3 columns) so pads and reverse suffixes are easy to check by hand.
Peak i
Spaces (j > i)
Letters
Printed row
'A'
2
A
A
'B'
1
BA
BA
'C'
0
CBA
CBA
When i = 'B', column C is a space; then B and A print → BA.
Code
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;
}
Output
A
BA
CBA
DCBA
EDCBA
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;
}
Output (when user enters D)
Enter the top letter (like E): D
A
BA
CBA
DCBA
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;
}
Output
A
BA
CBA
DCBA
EDCBA
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.