Right-Aligned Sequential Pyramid in C

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

What You’ll Learn

Like program 13, one counter k walks the alphabet, but each row is padded on the left so the growing run sits flush on the right.

Use a monospace font when running the program so %2c columns line up.

⭐ Pattern Output

Five rows (spacing from " " + %2c):

Output
        A
      B C
    D E F
  G H I J
K L M N O
1

Complete C Program ('A''E')

Same logic with int k = 65 and for (i = 65; i <= 69; i++) as in the reference.

c
#include <stdio.h>

int main() {
    int i, j;
    char k = 'A';

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

    return 0;
}

🧠 How It Works

1

k = 'A' streams the alphabet

Unlike a triangle that restarts each row, k keeps increasing. Every real letter print uses k++, so the pattern reads as one continuous A, B, C, … sequence laid into a grid.

Stream
2

Outer i sets how many letters appear

i is the row’s peak letter. Row A exposes one letter slot, row B two, up to five on row E. The rest of the fixed-width line is padded.

Width
3

Inner j walks columns right to left

For each column index j from E down to A, if j > i you print two spaces (an empty %2c cell). Otherwise you print the next streamed letter with printf("%2c", k++) so columns line up.

Grid
4

Why the grid is five cells wide

You always iterate across the full letter range, so each row has five logical columns. Doubled spaces plus %2c make the staircase align the same way as the sample output on any screen width.

Align
=

Letter count

Rows print 1+2+3+4+5 = 15 letters, ending at O. Padding loops add more writes, but time still scales as O(n²) for n letters in this fixed-grid style.

2

Variation — User Input

endChar = (char)('A' + rows - 1); inner j runs endChar down to 'A'.

c
#include <stdio.h>

int main() {
    int rows;
    int i, j;
    char k = 'A';
    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 >= startChar; --j) {
            if (j > i) {
                printf("  ");
            } else {
                printf("%2c", k++);
            }
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Tighter layout: one space + %c (then adjust padding to match)
  • Print a leading space between pairs with " " after %2c for readability
  • Ensure rows so the last letter stays ≤ 'Z'

Avoid

  • Mixing single-space padding with %2c without checking alignment in your terminal
  • Resetting k each row (you would repeat A, AB, …)

Key Takeaways

1

k++ only when a letter is printed; order follows the inner loop.

2

j > i reserves left columns; %2c fills right columns evenly.

3

Total letters for n rows from A: n(n+1)/2.

4

O(n²) inner steps for n rows.

❓ Frequently Asked Questions

Minimum field width 2: the implementation pads the character (usually with a leading space) so each letter occupies two columns.
Yes, but then match padding: often one space in the j > i branch so every slot stays two columns wide.
Program 16 centers a growing odd-width line with spaces between letters. Here letters are consecutive in k and the row is right-justified in a fixed grid.
O(n²) for n rows with an n-wide inner scan.

Explore More C Alphabet Patterns!

Pairing a format width on letters with fixed-width padding is a simple way to grid-align console output.

All Alphabet Patterns →
Did you know?

Row i (as the rth letter from A) prints exactly r new characters; the running total after n rows is the nth triangular number.

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