Right-Aligned Alphabet Pyramid in C

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

Pad with ordinary spaces, then print Ai using %2c. Same family as program 22 (grid + %2c), but here each row repeats from A and uses one space per padding step.

Use a monospace font so alignment holds.

⭐ Pattern Output

Five rows; bottom row is the widest:

Output
    A
   A B
  A B C
 A B C D
A B C D E
1

Complete C Program ('A''E')

Equivalent to loops on 6569 with j > i for spaces.

c
#include <stdio.h>

int main() {
    int 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 i grows the letter run

i walks AE. It marks how many letters belong on the row: row A prints one letter, row B prints two, and so on, while staying right-aligned.

Row
2

Leading spaces from high columns

Scan j from E down to i + 1. Each step prints one ordinary space. The count is 'E' - i, which shrinks as the letter block grows, pushing the ramp to the right edge.

Pad
3

Letters A through i with %2c

The second loop prints every letter from the start of the alphabet up to the row peak. Width-two formatting keeps single letters aligned in columns so the staircase reads clearly on narrow viewports.

%2c
4

Constant visual width

Spaces plus doubled letters keep the right margin fixed: the top row is mostly padding, the bottom row uses five wide letter slots. Total non-newline output per row stays predictable for layout.

Align
=

Example row

For i = 'C' you print two padding spaces, then %2c for A, B, C. Five rows × O(n) inner work → O(n²) overall for n letters.

2

Variation — User Input

endChar = (char)('A' + rows - 1); padding loop uses j > i with j starting at endChar.

c
#include <stdio.h>

int main() {
    int rows;
    int i, j, k;
    char startChar, endChar;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    startChar = 'A';
    endChar = (char)('A' + rows - 1);

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Left-align: drop the space loop
  • Center: split padding roughly before and after the letter run
  • Match padding width to %3c if you widen letters

Avoid

  • Mixing double-space padding with %2c without rechecking the grid
  • Expecting perfect alignment in a proportional font

Key Takeaways

1

Padding count per row: endChar - i single spaces.

2

Letter count per row: i - startChar + 1.

3

%2c pairs with monospace output for even columns.

4

O(n²) work for n rows.

❓ Frequently Asked Questions

You want spaces only while j is strictly past the current row letter. When j == i, stop so the next character printed is the start of the letter run.
You could, but then halve the padding iterations or the diagram shifts; this program uses one space per loop step by design.
Program 26 keeps five letters per row in a rotating order with plain %c. Here each row grows Ai and is pushed right with spaces and %2c.
O(n²) for n rows.

Explore More C Alphabet Patterns!

Single-space padding plus wide letter fields is a simple recipe for a right-justified growing triangle.

All Alphabet Patterns →
Did you know?

Each step down removes one padding iteration and adds one letter to the run; with %2c and a monospace font, the longest row defines the right margin.

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