Centered Alphabet Pyramid in C

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

What You’ll Learn

Three rows, odd-width letter groups, padded with spaces so the block lines up like a pyramid. Letters flow in order through one counter k.

Related: program 14 (odd-width triangle without centering) and star pyramids (spaces + symbols).

⭐ Pattern Output

Three rows (spaces between letters; leading spaces align columns):

Output
    A
  B C D
E F G H I
1

Complete C Program (Three Rows)

Bottom row ends at 'E' (69); outer i runs 65, 67, 69. Inner j runs 69 down to 65.

c
#include <stdio.h>

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

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

    return 0;
}

🧠 How It Works

1

k = 65 streams letters

Like other “flood” triangles, k never resets. Each real letter print uses k++, so the pattern reads as one continuous A, B, C, … sequence dropped into a grid.

Stream
2

Outer i += 2 picks odd caps

The row anchor jumps 65 → 67 → 69 (A, C, E). That makes each row wider by two letter slots while keeping the cap on odd letters only.

Rows
3

Inner j walks the full top row width

j counts from 69 down to 65 every time. When j > i you print a space; otherwise you print the next streamed letter with printf("%c", k++). Fixed-width scanning keeps right alignment.

Grid
4

First row detail

For i = 65, j = 69,68,67,66 are all > i (four spaces); j = 65 prints A.

Center
=

Inner work per row

This sample always runs five inner steps; for general rows the inner width is 2 * rows - 1 positions per line, so total work is O(r²).

2

Variation — User Input

Last outer i is 65 + 2*(rows-1); inner j starts at the same value. Same j > i rule.

c
#include <stdio.h>

int main() {
    int rows, i, j, k;

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

    k = 65;

    for (i = 65; i <= 65 + 2 * (rows - 1); i += 2) {
        for (j = 65 + 2 * (rows - 1); j >= 65; --j) {
            if (j > i) {
                printf(" ");
            } else {
                printf("%c ", k++);
            }
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Rewrite with for (i = 'A'; i <= 'E'; i += 2) and matching j bounds for readability
  • Two-loop style: print (last - i) / 2 spaces, then a dedicated letter loop (same geometry, clearer intent)
  • Clamp rows so k does not pass 'Z'

Avoid

  • Confusing j > i with odd-only j — every integer j between bounds is visited
  • Resetting k each row (you would repeat A every line)

Key Takeaways

1

Fixed-width inner scan + j > i produces leading padding.

2

k++ only on printed letters keeps the alphabet continuous.

3

Outer step 2 gives 1, 3, 5, … letters per row.

4

O(r²) for r rows with width scaling with r.

❓ Frequently Asked Questions

For i = 65, values j = 69, 68, 67, 66 satisfy j > i, so you print four spaces; j = 65 prints the letter.
65 + 2*(rows-1) is the ASCII code of the last row’s end letter when rows is 3 that is 69 ('E').
Use printf("%c", k++). Centering logic is unchanged; only the letter spacing tightens.
O(r²) for r rows in the generalized version.

Explore More C Alphabet Patterns!

Centering is often “print padding, then print the payload” — here both share one inner loop.

All Alphabet Patterns →
Did you know?

When tracing row 1, list every j from 69 down to 66 — each is > 65, so you get four padding spaces, not only two values.

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