Right-Aligned Reverse Pyramid in C

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

Each row is a reverse suffix (A, BA, CBA, …) padded on the left so the letters line up on the right in a fixed-width column. This is the same j > i idea as the left half of Program 19, but without the second mirror loop. Compare Program 2 (reverse, left-aligned). Includes a live preview, worked C examples, edge cases, and complexity.

Shape Rule

Right-aligned reverse

Growing reverse suffixes sit on the right of a fixed width.

Outer Loop

Row peak

i walks A..top so the visible suffix grows each row.

Inner Scan

Fixed width

j always walks top down to A for every row.

Pad Rule

j > i

Print a space while above the peak; then print letters.

Live Preview

Top letter

Pick A–J and draw the right-aligned pyramid instantly.

O(n²)

Complexity

n rows × n columns per fixed-width scan.

Introduction

A right-aligned reverse alphabet pyramid keeps every row the same width and fills the left with spaces until the reverse suffix begins — so A, BA, CBA, … line up on the right edge.

In C you solve it with nested char loops: outer i grows the peak, inner j scans top..A, and j > i decides space vs letter.

Why it matters?

It combines three beginner skills: fixed-width scans, leading-space padding, and descending letter order — the same toolkit used for many right-aligned pyramids.

Key Highlights

Fixed Width

Every row scans top..A columns.

Leading Spaces

j > i pads until the suffix starts.

Reverse Suffix

Descending j prints BA, CBA, DCBA…

Growing Peak

Outer i from A to top lengthens the suffix.

In short: for each peak i, scan top..A — print a space while j > i, otherwise print j, then call printf("\n").

📝 Problem & Approach

Given a top letter (like E), print a right-aligned pyramid of reverse alphabet suffixes.

c
// Classic sample (top = E; leading spaces matter)
//     A
//    BA
//   CBA
//  DCBA
// EDCBA

Inputs & Outputs

ItemTypeDescription
topcharHighest letter (e.g. E). Line width = top − ‘A’ + 1.
Printed outputtextRight-aligned reverse suffixes with leading spaces.

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
Fixed-width scanSpace-or-letter in each columnMatching this classic sample
Explicit pad + suffixPrint spaces, then i..A reverseClearer reading / teaching rewrite

⚡ Quick Reference

GoalPattern
Row peaksfor (char i = 'A'; i <= top; i++)
Fixed scanfor (char j = top; j >= 'A'; j--)
Pad vs letterif (j > i) printf(" "); else printf("%c", j);
End the rowprintf("\n");
Left-aligned reverseSee Program 2
Add right mirrorSee Program 19

📋 Space vs Letter vs Newline

Same fixed-width row — different roles on each column.

printf(" ")
j > i

Leading pads that create right alignment

printf("%c", j)
j <= i

Reverse suffix letters for the current peak

j--
E..A

Inner direction makes BA, CBA, DCBA…

printf("\n")
break

Ends the row after the full width scan

Context

When This Pattern Shows Up

Reach for this when teaching right alignment with reverse letter fills.

  1. After Program 19

    Keep one half of the dual scan — the pad-and-suffix idea alone.

  2. Alignment drills

    Practice leading spaces on a fixed-width console line.

  3. Compare with Program 2

    Same reverse letters; left-aligned vs right-aligned layout.

  4. Before diamond labs

    Padding intuition helps when you later center rows.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one condition (j > i) turns a flat reverse scan into a right-aligned pyramid.

🔮 Live Preview

Enter a top letter from A to J and draw the right-aligned reverse pyramid in the browser.

Try E (classic sample) or D (smaller). Use a single letter A–J for a readable preview.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C programs — fixed top E, scanf top letter, and explicit pad + suffix. . Click View Output to reveal sample console results.

📚 Getting Started

Print five right-aligned reverse rows with a fixed-width scan.

Example 1 — Fixed Top E

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

c
#include <stdio.h>

int main() {
    int i, j;

    for (i = 'A'; i <= 'E'; ++i) {
        for (j = 'E'; j >= 'A'; --j) {
            if (j > i) {
                printf(" ");
            } else {
                printf("%c", j);
            }
        }
        printf("\n");
    }

    return 0;
}

How It Works

When i = 'C', columns E and D print spaces; then C, B, A print → ··CBA. When i = 'E', every column is a letter → EDCBA.

📈 Practical Variant

Let the user choose the last letter.

Example 2 — Top Letter Input

The pattern keeps the line width fixed to the chosen top letter. Check scanf’s return value and require A–Z in real apps.

c
#include <stdio.h>

int main() {
    char top;
    int i, j;

    printf("Enter the top letter (like E): ");
    scanf(" %c", &top);

    for (i = 'A'; i <= top; ++i) {
        for (j = top; j >= 'A'; --j) {
            if (j > i) {
                printf(" ");
            } else {
                printf("%c", j);
            }
        }
        printf("\n");
    }

    return 0;
}

How It Works

Same j > i rule; only the shared bounds follow top. With top = 'D' you get a 4-column right-aligned pyramid.

⚡ Explicit Style

Same shape with separate pad and suffix loops.

Example 3 — Pad Spaces, Then Reverse Suffix

Often clearer to read: print leading spaces first, then letters from the peak down to A.

c
#include <stdio.h>

int main() {
    char top = 'E';
    int width = top - 'A' + 1;
    char i, ch;
    int s, letters, pad;

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

        for (s = 0; s < pad; ++s) {
            printf(" ");
        }
        for (ch = i; ch >= 'A'; --ch) {
            printf("%c", ch);
        }
        printf("\n");
    }

    return 0;
}

How It Works

Peak i needs i - 'A' + 1 letters and width - letters leading spaces. The suffix loop prints i down to A — same visual pyramid as the scan version.

🧠 How the Algorithm Prints Rows

1

Outer loop chooses the row peak

i moves from A to top, increasing the visible suffix each time.

Rows
2

Inner loop scans the full width

j runs from top down to A, giving a fixed-width line.

Width
3

Spaces create right alignment

If j > i print a space; otherwise print j. Leading spaces push the suffix to the right edge.

Align
4

New line

printf("\n") ends the row so the next peak starts fresh.

Break
=

Fixed width, growing suffix

For n letters, total work is O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — top E

Trace each peak i and how many pads vs letters print.

iLeading spacesSuffixPrinted row
A4A····A
B3BA···BA
C2CBA··CBA
D1DCBA·DCBA
E0EDCBAEDCBA

Pad count is top - i (as char distance). Each row still scans 5 columns.

Use Cases

Where this right-aligned reverse pyramid shows up beyond the homework prompt.

1. Alignment Practice

Clearest alphabet demo of leading spaces on a fixed width.

Example: print . instead of spaces while debugging.

2. Pair with Program 2

Same reverse letters — left-aligned vs right-aligned.

Example: print both for top E side by side.

3. Half of Program 19

This scan is the left half of the mirrored-gap pattern.

Example: add a right mirror pass next.

4. Explicit Pad Rewrite

Teach pad count separately from the reverse suffix (Example 3).

Example: compare scan vs pad+suffix outputs.

5. Complexity Intuition

Fixed-width scans make O(n²) easy to count.

Example: 5 rows × 5 columns = 25 writes.

6. Char Validation

Practice reading and validating a single top letter.

Example: reject empty strings and non A–Z input.

Pro Tip: say “pad while above the peak, then print reverse letters” before coding — that story prevents flipped alignment.

Advantages

Why this pattern earns a spot after left-aligned reverse triangles.

  1. 1. Instant Visual Feedback

    Missing pads or a flipped inner loop show up as a broken pyramid immediately.

  2. 2. Two Clear Rewrites

    Fixed-width scan or explicit pad/suffix loops teach the same shape.

  3. 3. Builds Toward Program 19

    Master one half before adding the mirrored right ramp.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop variables.

Pro Tip: learn the classic scan version first; treat the explicit pad/suffix rewrite as a clarity upgrade afterward.

Usage Tips

Small habits that keep right-aligned reverse pyramids clean.

  1. 1. Keep a Fixed Inner Width

    Always scan top..A so shorter suffixes stay right-aligned.

  2. 2. Use Spaces, Not Tabs

    Tabs change width by editor settings and ruin alignment.

  3. 3. Validate Letter Input

    Require a single A–Z character; empty scanf breaks scanf.

  4. 4. Debug with Dots

    Temporarily print . instead of spaces to count the pads.

  5. 5. Dry-Run Peak C

    Trace two spaces then CBA on paper before coding larger tops.

Pro Tip: if letters sit on the left with trailing spaces, you almost certainly flipped the j > i condition.

Common Pitfalls

Mistakes that commonly break right-aligned reverse alphabet pyramids.

  1. 1. Flipping the Pad Condition

    Using j < i for spaces left-aligns or garbles the suffix.

    → Print a space when j > i.

  2. 2. Scanning Upward

    Going A..top prints forward letters (AB, ABC) instead of reverse suffixes.

    → Keep j descending from top to A.

  3. 3. Tabs Instead of Spaces

    Alignment depends on the editor’s tab size.

    → Always print a single space character.

  4. 4. Unchecked scanf

    Empty or non-letter input leaves top invalid.

    → Check scanf’s return value and require A–Z.

  5. 5. printf("\n") Mid-Scan

    Breaks the row into one character per line.

    → Call printf("\n") only after the full width finishes.

Edge Cases

Check these inputs before calling the solution done.

top = A

Single letter

Output is just A (no pads).

top = E

Classic sample

Five rows ending in EDCBA.

top = D

Smaller pyramid

Four columns; last row DCBA.

Past Z

Invalid top

Reject or cap so letters stay in A–Z.

Bad input

Empty scanf

Unchecked scanf fails silently — check the return value.

Pad mark

. instead of space

Same loops; only the pad character changes.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Drop the padding

  • Print reverse suffixes left-aligned
  • Compare with Program 2

2. Add the right mirror

3. Explicit pad version

  • Use pad count + suffix (Example 3)
  • Confirm output matches the scan

4. Continue to Program 21

  • Diamond alphabet with alternating stars
  • See Program 21

Notes

  • Fixed width. Every row scans the same top..A columns.
  • j > i creates leading spaces; descending j creates reverse suffixes.
  • This is Program 19’s left half without the right mirror pass.
  • Prefer spaces over tabs for stable monospace alignment.

Quick Takeaway: scan top..A, pad while above the peak, print the reverse suffix, then break the line — that is the whole pyramid.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Fixed-width scan (Examples 1–2)O(n²)O(1)
Explicit pad + suffix (Example 3)O(n²)O(1)

With n = top − ‘A’ + 1, each of n rows scans n columns (or pads + letters totaling n), so work is O(n²).

Wrap Up

🎉 Conclusion

The right-aligned reverse alphabet pyramid is a small nested-loop exercise with lasting payoff: fixed-width scans, leading-space padding, and descending letter fills. Master the classic ····A…EDCBA sample, then try user input and the explicit pad rewrite.

Practice the three examples above, then continue to Program 21’s diamond alphabet pattern with alternating stars.

Scan top..A, print spaces while j > i, print letters otherwise, and break only after the scan.

💡 Best Practices

✅ Do

  • Scan a fixed top..A width every row
  • Print spaces when j > i
  • Keep the inner loop descending for reverse suffixes
  • Validate a single A–Z character on input
  • State O(n²) when asked about complexity

❌ Don’t

  • Flip j > i unless you want left alignment
  • Use tabs for padding
  • Call printf("\n") inside the column scan
  • Assume empty input is safe for scanf
  • Scan upward if you want BA, CBA, DCBA…

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the right-aligned reverse alphabet pyramid the beginner-friendly way.

5
Core concepts
W 02

Width

Always top..A

Code
> 03

Pad

j > i → space

Code
04

New line

Ends each scan

I/O
O 05

Complexity

O(n²) time

Analysis

❓ 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.
Update the loop bounds so the outer loop runs up to H and the inner scan starts from H down to A.
printf("%c", j) (or a space) stays on the same line for each cell. printf("\n") ends the row after the fixed-width scan finishes.
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 scanf(" %c", &top), require A–Z, and reject non-letters. Cap at Z if you only want alphabetic ranges.

Did you Know? 🔊

Each line has fixed width (five columns for AE). 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.

Continue to Alphabet Pattern 21

Next up: diamond patterns that mix letters and stars.

Program 21 tutorial →

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