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 suffixes (A, BA, CBA, …) on fixed-width rows, with leading spaces so the letters sit on the right.

Remember
Rule: scan top..A; print space while j > i, else print j

    A
   BA
  CBA
 DCBA
EDCBA     ← top = E

Unlike Program 2 (left-aligned reverse prefixes E, ED, EDC…), this pattern grows reverse suffixes from the right using the j > i space test.

How to Solve It

Pick a row peak, scan the full width downward, and print spaces until the peak — then print the reverse letters.

MethodIdeaBest for
Fixed-width scanInner j from top..A; j > i → space else letterLearning one-loop alignment
Explicit padPrint width - letters spaces, then i..AClearer reading once the shape is clear

Pseudocode

Pseudocode
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
Row peakfor (i = 'A'; i <= top; i++)
Fixed-width scanfor (j = top; j >= 'A'; j--)
Space or letterif (j > i) printf(" "); else printf("%c", j);
Explicit padpad = width - (i - 'A' + 1);
Reverse suffixfor (ch = i; ch >= 'A'; ch--) printf("%c", ch);
End the rowprintf("\n");

Printing Letters vs Starting a New Line

APIEffectUse for
printf(" ") / printf("%c", j)Stays on the same lineEach space and each letter
printf("\n")Ends the current lineAfter the fixed-width scan

Print each cell without a newline, then end the row once.

Live Preview

Change the top letter and the right-aligned reverse pyramid updates instantly — including width and row count.

Enter one letter A–J. Line width equals top - 'A' + 1; each row is that many columns.

Live result top E · width 5 · 5 rows
    A
   BA
  CBA
 DCBA
EDCBA

Worked Walkthrough — top = 'E'

Trace each peak i, how many pads print, and the reverse suffix that lands on the right.

iPadsSuffixPrinted row
A4AA
B3BABA
C2CBACBA
D1DCBADCBA
E0EDCBAEDCBA

Every row is width 5. Spaces appear while j > i; letters fill from the peak down to A.

C Programs

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

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(void)
{
    char 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

1. Outer loop picks the peak. i walks A..E — the first letter of the visible reverse suffix.

2. Inner loop scans fixed width. While j > i, print a space; otherwise print j. When i = 'C', that is ··CBA.

3. Full bottom row. When i = 'E', every column is a letter → EDCBA.

Example 2 — Top Letter Input

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

C
#include <stdio.h>

int main(void)
{
    char top, 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

1. Same j > i rule. Only the shared bounds follow top instead of the literal 'E'.

2. Width scales with top. With top = 'D' you get a 4-column right-aligned pyramid.

3. Safer input tip. Prefer a single uppercase letter:

Safer input
#include <ctype.h>

if (scanf(" %c", &top) != 1)
{
    printf("Enter one letter A–Z.\n");
    return 1;
}
top = (char)toupper((unsigned char)top);
if (top < 'A' || top > 'Z')
{
    printf("Enter one letter A–Z.\n");
    return 1;
}

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(void)
{
    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

1. Compute pad and length. Peak i needs i - 'A' + 1 letters and width - letters leading spaces.

2. Two clear loops. First pad, then print i down to A — same visual pyramid as the scan version.

3. When to use which. Keep the scan form for exams that ask one inner loop; use explicit pad when readability matters more.

Edge Cases & Pitfalls

Check these before calling the solution done.

j from A

Forward letters

Scanning upward prints AB, ABC, … instead of reverse suffixes. Keep j descending from top.

No spaces

Left-aligned

Skipping the j > i space branch left-aligns the reverse suffixes — you lose the right-aligned pyramid.

\n early

Broken row

Call printf("\n") only after the scan. Inside the inner loop, each cell lands on its own line.

top = 'A'

Single A

Output is just A (no pads) — a good sanity check.

Wrong compare

j >= i spaces

Using j >= i for spaces skips the peak letter. Spaces belong only while j > i.

Bad scanf

Uninitialized top

Check scanf(" %c", &top) == 1 before looping so a failed read does not leave top garbage.

Time and Space Complexity

ProgramTimeExtra space
Scan / explicit pad formsO(n²)O(1)

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

Key Takeaways

  • Fixed width: every row scans top..A so alignment stays consistent.
  • Space test: print space while j > i; otherwise print the reverse letter.
  • Vs Program 2: reverse suffixes on the right, not reverse prefixes on the left.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each peak, scan top..A and print spaces until the peak, then the reverse suffix.

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

Alphabet diamond rows with stars filling the outer frame.

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