Centered Alphabet Pyramid in C

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

Print a centered pyramid: one letter on the first row, then 3 letters, then 5 letters, with leading spaces so it looks aligned. Letters flow continuously via one counter: A, then B C D, then E F G H I. Compare Program 14 (odd widths, no centering) and Program 13 (sequential, left-aligned). Includes a live preview, worked C examples, edge cases, and complexity.

Shape Rule

Odd widths, centered

Rows print 1, 3, 5… letters under a fixed bottom width.

Outer Loop

Step by 2

for (int i = 1; i <= width; i += 2) picks each odd row width.

Inner Scan

Pad then letters

When j > i print a space; otherwise print the next letter.

Running Counter

Never reset

One k (or index) walks A, B, C… across the whole pyramid.

Live Preview

1–5 rows

Pick a pyramid height and draw it instantly in the browser.

O(r²)

Complexity

Each row scans O(width) columns; overall work is O(r²).

Introduction

A centered alphabet pyramid grows by two letters on each new line and pads the left with spaces so shorter rows sit under the widest row. Letters stay consecutive across the whole shape — they do not restart at A each row.

In C you usually solve it with nested loops: the outer loop steps odd widths, the inner loop scans a fixed bottom width printing spaces or the next letter, then printf("\n") ends the row.

Why it matters?

It combines three beginner skills at once: odd-width growth, leading-space centering, and a continuous letter counter — the same toolkit used for many pyramids and diamonds.

Key Highlights

Odd Letter Counts

Rows print 1, 3, 5, … letters.

Leading Spaces

Pad left so short rows stay centered.

Continuous Letters

One counter never resets between rows.

Fixed Scan Width

Inner loop always walks the bottom width.

In short: scan each odd width with pads where j > i, print consecutive letters with printf("%c ", k++), then printf("\n").

📝 Problem & Approach

Given an odd bottom width (like 5) or a row count, print a centered pyramid of consecutive alphabet letters with leading spaces.

c
// Three rows (conceptual shape; spaces matter)
//     A
//   B C D
// E F G H I

Inputs & Outputs

ItemTypeDescription
width / rowsintOdd bottom width (1, 3, 5, …) or number of pyramid rows. Width = 2*rows - 1.
Printed outputtextCentered odd-width rows of consecutive letters with leading spaces.

Minimal workflow

Pseudocode
k = 'A' (or index 0 into A..Z)
for i in 1, 3, 5, ... width:
    for j from width down to 1:
        if j > i: print space
        else: print next letter (+ optional trailing space)
    print newline

Approach comparison

ApproachIdeaBest for
Fixed-width scanInner loop always walks width columnsMatching this classic sample
Explicit pad + lettersPrint (width-i) spaces, then i lettersClearer reading / teaching rewrite

⚡ Quick Reference

GoalPattern
Odd row widthsfor (i = 1; i <= width; i += 2) or ASCII 65..69
Scan columnsfor (j = width; j >= 1; --j)
Leading padif (j > i) printf(" ");
Next letterprintf("%c ", k++);
End the rowprintf("\n");
No centeringSee Program 14

📋 Space vs Letter vs printf

Same pyramid — different roles on each inner-loop pass.

printf(" ")
pad

Printed while j > i to center the row

printf("%c ", k++)
fill

Printed when inside the current odd width

k++
next

Advances the continuous alphabet stream

printf("\n")
break

Ends the row after the full column scan

Context

When This Pattern Shows Up

Reach for this pyramid when teaching centering and continuous fills together.

  1. After Program 14

    Keep odd widths; add leading spaces for a centered look.

  2. Padding drills

    Practice left padding the same way star pyramids do.

  3. Continuous counters

    Reuse the running-letter idea from Program 13 with centering.

  4. Gateway to diamonds

    Once centering clicks, inverted and full diamond shapes follow.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in odd-width growth, padding, and continuous fill at the same time.

🔮 Live Preview

Choose a pyramid height between 1 and 5 rows (bottom width = 2×rows−1) and draw it in the browser.

Try 3 (width 5, through I) or 4 (width 7, through P). Max 5 keeps letter count within A–Y.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C programs — fixed width 5, odd-width input, and an explicit pad-then-letters rewrite. Click View Output to reveal sample console results.

📚 Getting Started

Print a three-row pyramid with a fixed-width scan.

Example 1 — Fixed bottom width 5

Hard-coded bounds — ideal for first demos and screenshots.

c
#include <stdio.h>

int main() {
    int i, j;
    int 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

When i = 65 ('A'), four columns print spaces and one prints A. When i = 67 ('C'), two spaces then B C D. When i = 69 ('E'), the full width prints E F G H I.

📈 Practical Variant

Let the user choose an odd bottom width.

Example 2 — Odd Width Input

Read the bottom width as an odd number (like 5 or 7). Check scanf and odd validation in real apps.

c
#include <stdio.h>

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

    printf("Enter the bottom width (odd number): ");
    scanf("%d", &width);

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

    return 0;
}

How It Works

Same centering scan as Example 1; only the outer/inner bounds follow width. Require an odd width so rows stay 1, 3, 5, … under a matching bottom line.

⚡ Explicit Style

Same pyramid with separate pad and letter loops.

Example 3 — Pad Spaces, Then Letters

Often clearer to read: print leading spaces first, then the odd letter count.

c
#include <stdio.h>

int main() {
    int rows = 3;
    int width = 2 * rows - 1;
    char k = 'A';
    int row, letters, pad, s, L;

    for (row = 1; row <= rows; ++row) {
        letters = 2 * row - 1;
        pad = width - letters;

        for (s = 0; s < pad; ++s) {
            printf(" ");
        }

        for (L = 0; L < letters; ++L) {
            printf("%c", k);
            if (L < letters - 1) {
                printf(" ");
            }
            ++k;
        }

        printf("\n");
    }

    return 0;
}

How It Works

Row r needs 2r-1 letters and width - letters leading spaces. Spaces between letters are separators only on the letter loop — same visual pyramid as the scan version.

🧠 How the Algorithm Prints Rows

1

Set up

#include <stdio.h> brings in printf / scanf. Create a running letter counter (k = 65 or char k = 'A').

Setup
2

Outer loop (odd widths)

i runs 1, 3, 5… — how many letters appear on the row.

1, 3, 5
3

Inner scan (pad / letter)

Walk the bottom width. If j > i, print a space; else print the next letter and advance the counter.

Padding
4

New line

printf("\n") ends the row so the next odd width starts fresh.

Break
=

Pyramid complete

r rows scan O(width) columns each — O(r²) time, O(1) extra memory.

🔎 Worked Walkthrough — width 5

Trace each outer value of i and see how many pads vs letters print.

iLeading spacesLettersPrinted row
14A····A
32B C D··B C D
50E F G H IE F G H I

Total letters: 1 + 3 + 5 = 9 (A through I). Pads: 4 + 2 + 0 = 6.

Use Cases

Where this centered pyramid (and its padding idea) shows up beyond the homework prompt.

1. Centering Practice

Clearest alphabet demo that leading spaces create a pyramid.

Example: remove pads and watch rows snap left.

2. Pair with Program 14

Same odd widths — with or without centering.

Example: side-by-side left-aligned vs padded.

3. Continuous Fill Labs

Keep a running counter across padded rows.

Example: reset k each row and compare to Program 1-style prefixes.

4. Star Pyramid Transfer

Same pad math works if you print * instead of letters.

Example: swap letter prints for printf("* ").

5. Complexity Intuition

Fixed-width scans make O(r²) easy to count.

Example: 3 rows × 5 columns = 15 inner iterations.

6. Odd-Input Validation

Practice requiring odd widths before drawing.

Example: reject even width and re-prompt.

Pro Tip: say “pad first, then consecutive letters” before coding — that story prevents resetting k or forgetting spaces.

Advantages

Why this pattern earns a spot after left-aligned odd-width triangles.

  1. 1. Instant Visual Feedback

    Missing pads or a reset counter show up immediately as a broken pyramid.

  2. 2. Transfers to Star Pyramids

    Same padding logic works for classic * pyramids.

  3. 3. Two Clear Rewrites

    Fixed-width scan or explicit pad/letter loops teach the same shape.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond counters.

Pro Tip: learn the classic scan version first; treat the explicit pad/letter rewrite as a clarity upgrade afterward.

Usage Tips

Small habits that keep centered pyramid code clean.

  1. 1. Require Odd Widths

    Use 1, 3, 5, … so the pyramid stays symmetric under the bottom row.

  2. 2. Keep the Counter Outside

    Do not reset k each row if you want continuous letters.

  3. 3. Check scanf

    Avoid crashes when the user types letters instead of a number.

  4. 4. Watch the Alphabet Cap

    Width 9 uses 25 letters (A–Y); larger bottoms may pass Z.

  5. 5. Dry-Run Width 5

    Trace pads 4 / 2 / 0 on paper before coding larger demos.

Pro Tip: if every row starts with A, you almost certainly reset the letter counter inside the outer loop.

Common Pitfalls

Mistakes that commonly break centered alphabet pyramids.

  1. 1. Forgetting Leading Spaces

    Rows shift left and no longer look like a pyramid.

    → Print pads while j > i (or print width - letters spaces first).

  2. 2. Resetting the Letter Counter

    Each row starts at A again — that is a different pattern.

    → Keep k outside the outer loop.

  3. 3. Even Bottom Width

    Even widths break the 1, 3, 5… symmetry under the base.

    → Require an odd width (or derive width = 2*rows - 1).

  4. 4. Unchecked scanf

    Letters or empty input leave width uninitialized.

    → Check scanf’s return value and re-prompt on failure.

  5. 5. Walking Past Z

    Large odd widths need more than 26 letters.

    → Cap width so 1+3+…+width ≤ 26, or define wrap/stop policy.

Edge Cases

Check these inputs before calling the solution done.

width = 1

Single letter

Output is just A on one line.

width = 5

Classic sample

Three rows through I.

Even width

Like 4 or 6

Reject or bump to next odd for a clean pyramid.

width = 9

Near Z

25 letters (A–Y) — last full A–Z-friendly odd width.

Bad input

Non-numeric scanf

Unchecked scanf fails silently — check the return value.

Case

Lowercase variant

Same loops work with k = 'a'.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Drop the centering

  • Print odd widths left-aligned
  • Compare with Program 14

2. Star pyramid

  • Same pads; print * instead of letters
  • Shows padding is reusable

3. Reset vs continuous

  • Reset k = 'A' each row once
  • See how the shape meaning changes

4. Continue to Program 17

  • Reverse alphabet with a diagonal star
  • See Program 17

Notes

  • Odd sums. Letter counts are 1+3+…+width; for width 5 that is 9 letters.
  • Leading spaces create the centered look; trailing letter spaces are optional separators.
  • Keep the running counter outside the outer loop for continuous letters.
  • Prefer odd bottom widths; width = 2*rows - 1 is a safe formula.

Quick Takeaway: step odd widths, pad on the left, print consecutive letters, then break the line — that is the whole pyramid.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Fixed-width scan (Examples 1–2)O(r²)O(1)
Explicit pad + letters (Example 3)O(r²)O(1)

With bottom width w = 2r-1, each of the r rows scans O(w) columns, so total work is O(r²).

Wrap Up

🎉 Conclusion

The centered alphabet pyramid is a small nested-loop exercise with lasting payoff: odd-width growth, leading-space centering, and a continuous letter stream. Master the classic fixed-width scan, then optionally rewrite it as explicit pad + letter loops.

Practice the three examples above, then continue to Program 17’s reverse alphabet with a diagonal star.

Use an odd bottom width, pad while outside the current width, advance one letter counter across all rows, and break only after the scan.

💡 Best Practices

✅ Do

  • Use odd widths (or width = 2*rows - 1)
  • Print leading spaces before letters on short rows
  • Keep the letter counter outside the outer loop
  • Check scanf and odd-width validation
  • State O(r²) when asked about complexity

❌ Don’t

  • Skip padding if you want a centered pyramid
  • Reset the letter counter each row for this pattern
  • Use an even bottom width without a clear policy
  • Ignore the A–Z letter budget on large widths
  • Call printf("\n") inside the letter loop

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the centered alphabet pyramid the beginner-friendly way.

5
Core concepts
+2 02

Outer loop

Widths 1, 3, 5…

Code
A 03

Counter

Continuous letters

Code
04

New line

Ends each scan

I/O
O 05

Complexity

O(r²) time

Analysis

❓ Frequently Asked Questions

The inner loop scans a fixed bottom width. While the column index is still outside the current row width, it prints spaces; otherwise it prints the next letter.
The outer loop increases by 2 (1, 3, 5), so each row prints an odd number of letters, forming a pyramid shape.
Increase the maximum odd width (and loop bounds). The number of rows grows as width grows: 1, 3, 5, 7...
They shift each row to the right so the odd-width lines are centered under the widest line.
The trailing space makes columns easier to see. For compact output, use printf("%c", k++) instead.
printf("%c ", k++) prints a letter (and optional space) on the same line. printf("\n") ends the current line after the column scan.
O(r²) for r rows because each row scans a fixed-width set of columns and prints a growing number of characters overall.
Check scanf("%d", &width) == 1 and require an odd positive width (1, 3, 5, …). Cap so total letters 1+3+…+width stay within A–Z if you want only alphabetic output.

Did you Know? 🔊

This pattern combines two ideas: odd-width rows (i += 2) and centering via padding spaces (like star pyramids). Letters flow continuously via one counter — A, then B C D, then E F G H I — while leading spaces keep each row aligned under the widest line.

Continue to Alphabet Pattern 17

Keep exploring alphabet and star mixes with nested loops.

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