C Alphabet Pyramid Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned alphabet pyramid prints growing prefixes of the alphabet (A, A B, A B C, …) pushed right with leading spaces so every row shares the same right edge.

Remember
Rule: for row letter i from A to top,
      print (top - i) spaces, then A..i with width 2

     A
    A B
   A B C
  A B C D
 A B C D E     ← top = 'E' (monospace)

Letters restart from A every row (like Program 1), but leading spaces shrink so the right edge stays fixed. Compare with Program 26 (fixed-length rotating rows).

How to Solve It

Two ways to emit the same shape — nested char loops with pad + %2c, or integer row indexes.

MethodIdeaBest for
Char loops + %2cPad top - i spaces; print A..iLearning, interviews, exams
Integer row indexesPad n - row spaces; letter = 'A' + LWhen you already think in row numbers

Pseudocode

Pseudocode
for i from 'A' to top:
    for j from top down to just above i:
        print space
    for k from 'A' to i:
        print k with width 2
    print newline

Cheat sheet

GoalPattern
Walk each row letterfor (i = 'A'; i <= top; i++)
Leading spacesfor (j = top; j > i; j--) printf(" ");
Print A..ifor (k = 'A'; k <= i; k++) printf("%2c", k);
End the rowprintf("\n");
Pad counttop - i spaces (char math)
Integer rewritePad n - row; letter (char)('A' + L)

Printing Letters vs Starting a New Line

APIEffectUse for
printf(" ")Stays on the same lineEach leading space
printf("%2c", k)Letter in a width-2 fieldEach letter on the row
printf("\n")Ends the current lineAfter pad and letter loops

Live Preview

Change the letter count (top = A + n - 1) and the right-aligned pyramid updates instantly — capped at 10 for readability.

Whole numbers from 1 to 10. With n = 5, top is E and you get the classic sample. Best viewed in monospace.

Live result 5 rows · 15 letters
     A
    A B
   A B C
  A B C D
 A B C D E

Worked Walkthrough — top = 'D'

Trace each row letter i with top fixed at 'D'. Pad count is top - i; letters use width 2.

Row iPad spacesLettersPrinted row
'A'3AA
'B'2A BA B
'C'1A B CA B C
'D'0A B C DA B C D

Letter prints: 1 + 2 + 3 + 4 = 10. Space prints add another triangular count — still O(n²) overall.

C Programs

Three complete programs: fixed top letter, scanf input, and an integer-index rewrite. Use View Output to reveal sample results.

Example 1 — Fixed top at 'E'

Hard-coded range — shrink leading spaces, then print A..i with %2c.

C
#include <stdio.h>

int main(void)
{
    char i, j, k;

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

    return 0;
}

How It Works

1. Outer loop picks the row letter. i runs from 'A' to 'E' — one row per end letter.

2. Pad loop shrinks. Print spaces while j counts from 'E' down past i — fewer spaces each row.

3. Letter loop restarts at A. printf("%2c", k) prints A through i in even columns.

4. Break the line. printf("\n") after both inner loops starts the next wider prefix.

Example 2 — User Input Version

Read the top letter at runtime with scanf. Prefer a leading space in the format and validate A–Z.

C
#include <stdio.h>

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

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

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

    return 0;
}

How It Works

1. Prompt and read. scanf(" %c", &top) — the leading space skips leftover newlines.

2. Same pad + prefix core. Only the upper bound changes from hard-coded 'E' to top.

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

Safer input
if (scanf(" %c", &top) != 1 || top < 'A' || top > 'Z')
{
    printf("Enter an uppercase letter A–Z.\n");
    return 1;
}

Example 3 — Integer row indexes

Same shape thinking in row numbers: pad n - row spaces, then print row letters from A.

C
#include <stdio.h>

int main(void)
{
    int n = 5;
    int row, s, L;

    for (row = 1; row <= n; row++)
    {
        for (s = 0; s < n - row; s++)
        {
            printf(" ");
        }
        for (L = 0; L < row; L++)
        {
            printf("%2c", (char)('A' + L));
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Map size to rows. n = 5 means five lines — same as top letter 'E'.

2. Pad with integers. n - row spaces on each line — identical shrinking pad.

3. Letter from index. (char)('A' + L) for L = 0..row-1 rebuilds A..i.

Edge Cases & Pitfalls

Check these before calling the solution done.

No pad

Left-aligned by mistake

Skipping the space loop reprints Program 1’s shape (plus %2c gaps). Keep the pad loop for right alignment.

%c only

Uneven columns

Plain printf("%c", k) still works but looks tighter. Use %2c when you want even letter columns.

\n early

Broken rows

Call printf("\n") only after both the pad and letter loops.

k++

Continuous stream

A single advancing letter across all rows is a different pattern. Restart k at 'A' every row.

top = 'A'

Single A

One row with no leading spaces and a single width-2 A — a good sanity check.

scanf

Leading space in format

Use scanf(" %c", &top) so a leftover newline is not read as the letter.

Time and Space Complexity

ProgramTimeExtra space
Char loops (Examples 1–2)O(n²)O(1)
Integer rows (Example 3)O(n²)O(1)

Each of n rows prints up to O(n) spaces and O(n) letters — quadratic in n.

Key Takeaways

  • Rule: shrink leading spaces, then print A..i with %2c.
  • Restart at A: every row begins the letter loop at 'A' again.
  • Break the row: call printf("\n") only after pad and letter loops.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each i, print top - i spaces, then A..i with %2c, then printf("\n").

Frequently Asked Questions

The first inner loop prints (top - i) leading spaces, fewer on each lower row, so the letter block ends at the same right edge.
Width-2 formatting prints each letter in an even column so the spaced layout lines up in a monospace font.
Program 1 is left-aligned with no padding and no spaces between letters (A, AB, ABC). This pattern adds leading spaces and uses %2c so columns stay even.
Remove the leading-space loop and print letters directly from A to the row letter.
printf("%2c", k) prints a letter (with padding to width 2) and stays on the same line. printf("\n") ends the current line after the pad and letter loops.
O(n²) for n rows because each row prints O(n) spaces plus O(n) letters.
Use scanf(" %c", &top) with a leading space, require A–Z, and reject non-letters.
Yes. Pad n − row spaces, then print row letters as (char)('A' + L). Example 3 on this page shows that style.

Did you know?

Leading padding: for each row letter i, print top - i spaces. Then print A through i with %2c so columns look even in monospace output. The last row has no padding; all rows share the same right edge.

Next: Symmetric Decreasing Square

Print a square of decreasing alphabet rows with mirrored sides.

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