Palindromic Alphabet Pyramid in C

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

What You’ll Learn

Each row is a palindrome: go up from A to the row’s peak letter, then come back down without repeating the peak — A, ABA, ABCBA, ABCDCBA, ABCDEDCBA. Compare Program 1 (left half only) and Program 16 (centered consecutive letters). Includes a live preview, worked C examples, edge cases, and complexity.

Shape Rule

Palindrome rows

Each row reads the same forward and backward.

Outer Loop

Row peak

Row r peaks at letter 'A' + r (0-based) or alpha[i].

Forward Half

A..peak

First inner loop prints up through the peak letter.

Mirror Half

peak-1..A

Start at peak - 1 so the center is not duplicated.

Live Preview

1–10 rows

Pick a height and draw the palindrome pyramid instantly.

O(n²)

Complexity

Odd row lengths sum to n² characters.

Introduction

A palindromic alphabet pyramid grows one peak letter per row and mirrors the left half so the full line reads the same both ways — without printing the peak twice.

In C you usually solve it with three loops: an outer row loop, a forward letter loop, and a reverse letter loop that starts at peak - 1.

Why it matters?

It teaches the classic “up then down, skip the center” mirror trick used in many palindrome and diamond patterns.

Key Highlights

Peak Letter

Row r peaks at the r-th letter.

Go Up

Print A through the peak.

Come Down

Mirror from peak - 1 to A.

Odd Lengths

Rows have 1, 3, 5, … letters.

In short: for each peak, print A..peak, then (peak-1)..A, then printf("\n") — never start the mirror at peak.

📝 Problem & Approach

Given a row count n (or fixed A–E), print a left-aligned pyramid where each row is an alphabet palindrome.

c
// Five rows (no spaces between letters)
// A
// ABA
// ABCBA
// ABCDCBA
// ABCDEDCBA

Inputs & Outputs

ItemTypeDescription
n / rowsintNumber of pyramid rows (1–26 for A–Z).
Printed outputtextPalindrome rows with odd lengths 1, 3, 5, …

Minimal workflow

Pseudocode
for each row r (0..n-1):
    peak = 'A' + r
    print 'A'..peak
    print (peak-1)..'A'
    print newline

Approach comparison

ApproachIdeaBest for
Index + char arrayalpha[j] / alpha[k]Matching this classic sample
Direct char loopsWalk letters without an arrayClearer user-input versions

⚡ Quick Reference

GoalPattern
Rows / peaksfor (i = 'A'; i <= endChar; ++i)
Forward halffor (j = 'A'; j <= i; ++j) printf("%c", j);
Mirror halffor (k = i - 1; k >= 'A'; --k) printf("%c", k);
Avoid double peakStart mirror at i - 1, not i
End the rowprintf("\n");
Left half onlySee Program 1

📋 Up vs Down vs printf

Same row — three different jobs.

A..peak
up

Builds the left half including the center

(peak-1)..A
down

Mirrors without repeating the peak

peak
once

Appears only from the forward loop

printf("\n")
break

Ends the row after both halves

Context

When This Pattern Shows Up

Reach for this when teaching mirror loops and palindrome rows.

  1. After Program 1

    You already print A..peak; now add the mirror half.

  2. Palindrome drills

    Practice skipping the center so mirrors stay clean.

  3. Gateway to diamonds

    Same up/down idea appears in number and star diamonds.

  4. Before Program 19

    Next you add a spacing gap between mirrored ramps.

  5. Not a UI layout tool

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

Key benefit: one careful start index (peak - 1) turns a left triangle into a full palindrome.

🔮 Live Preview

Choose between 1 and 10 rows and draw the palindromic alphabet pyramid in the browser.

Try 5 (classic A–E) or 4 (through D). Max 10 keeps the preview readable.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C programs — fixed A–E, scanf row count, and a centered variant with leading spaces. Click View Output to reveal sample console results.

📚 Getting Started

Print five palindrome rows with nested char loops.

Example 1 — Fixed A–E

Print the forward part A..i, then print back from i-1..A.

c
#include <stdio.h>

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

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

    return 0;
}

How It Works

When i = 'C', the forward loop prints ABC and the reverse loop starts at 'B'BA, giving ABCBA. Starting at i instead of i - 1 would wrongly print ABCCBA.

📈 Practical Variant

Let the user choose how many rows to print.

Example 2 — Row Count Input

Uses character bounds and endChar = 'A' + rows - 1. Check scanf in real apps.

c
#include <stdio.h>

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

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

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

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

    return 0;
}

How It Works

Outer i walks from 'A' to endChar. Cap rows at 26 so peaks stay within A–Z.

⚡ Layout Variant

Same palindromes, centered with leading spaces.

Example 3 — Centered Palindrome Pyramid

Add leading spaces so shorter rows sit under the widest row (same idea as Program 16).

c
#include <stdio.h>

int main() {
    int n = 5;
    int r, s;
    char peak, ch;

    for (r = 0; r < n; ++r) {
        peak = (char)('A' + r);

        for (s = 0; s < n - r - 1; ++s) {
            printf(" ");
        }

        for (ch = 'A'; ch <= peak; ++ch) {
            printf("%c", ch);
        }

        for (ch = (char)(peak - 1); ch >= 'A'; --ch) {
            printf("%c", ch);
        }

        printf("\n");
    }

    return 0;
}

How It Works

Pad with n - r - 1 spaces, then print the same up/down palindrome as before. The letter logic does not change — only alignment does.

🧠 How the Algorithm Prints Rows

1

Set up

#include <stdio.h> brings in printf / scanf. Choose a peak range such as 'A'..'E'.

Setup
2

Pick the row peak

Row i ends at alpha[i] or (char)('A' + r).

Peak
3

Print forward, then mirror

Print A..peak, then (peak-1)..A so the center appears once.

Up / Down
4

New line

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

Break
=

Pyramid complete

Odd lengths sum to n² — O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — 5 rows

Trace each row’s peak and both halves.

Row iPeakForwardMirrorPrinted row
0AA(empty)A
1BABAABA
2CABCBAABCBA
3DABCDCBAABCDCBA
4EABCDEDCBAABCDEDCBA

Lengths: 1 + 3 + 5 + 7 + 9 = 25 = 5².

Use Cases

Where this palindrome pyramid (and its mirror trick) shows up beyond the homework prompt.

1. Mirror Practice

Clearest alphabet demo of up-then-down without doubling the center.

Example: start mirror at peak once and see the double letter.

2. Pair with Program 1

Same forward half; this pattern completes the palindrome.

Example: print left-only vs full mirror side by side.

3. Number Palindromes

Same loops work with digits instead of letters.

Example: print 1, 121, 12321, …

4. Centering Labs

Add pads (Example 3) after the letter logic is solid.

Example: compare left-aligned vs centered output.

5. Complexity Intuition

Odd sums make the n² total easy to see.

Example: 5 rows print 25 letters total.

6. Alphabet Caps

Practice limiting input so peaks stay in A–Z.

Example: reject n > 26 or clamp it.

Pro Tip: say “up through the peak, then down from peak minus one” before coding — that story prevents doubled centers.

Advantages

Why this pattern earns a spot after left-half alphabet triangles.

  1. 1. Instant Visual Feedback

    A doubled peak shows up immediately as a non-palindrome.

  2. 2. Reusable Mirror Trick

    The same peak - 1 idea appears in many diamond labs.

  3. 3. Two Clear Styles

    Char arrays or direct char loops teach the same shape.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop variables.

Pro Tip: get the left-aligned palindrome right first; add centering pads only after the letters look correct.

Usage Tips

Small habits that keep palindrome-pyramid code clean.

  1. 1. Start Mirror at peak - 1

    That single off-by-one is the whole palindrome trick.

  2. 2. Keep Indexing Consistent

    0-based rows with 'A' + r avoid mixing 1-based peaks by accident.

  3. 3. Check scanf

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

  4. 4. Cap at 26 Rows

    Beyond Z you need a wrap/stop policy.

  5. 5. Dry-Run Row C

    Trace ABC + BA on paper before coding larger n.

Pro Tip: if you see doubled centers like ABCCBA, you almost certainly started the mirror at the peak.

Common Pitfalls

Mistakes that commonly break palindromic alphabet pyramids.

  1. 1. Starting Mirror at the Peak

    Duplicates the center (ABCCBA instead of ABCBA).

    → Begin the reverse loop at peak - 1.

  2. 2. Mixing 0-based and 1-based Rows

    Wrong peak letter on every row.

    → Stick to one scheme: r from 0 with 'A' + r.

  3. 3. Overflowing Z

    Large n walks past the alphabet.

    → Cap input at 26 or define a wrap policy.

  4. 4. Unchecked scanf

    Letters or empty input leave rows uninitializeduninitialized rows.

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

  5. 5. printf("\n") Inside a Half Loop

    Breaks the row into one character per line.

    → Call printf("\n") only after both halves finish.

Edge Cases

Check these inputs before calling the solution done.

n = 1

Single letter

Output is just A; mirror loop does not run.

n = 5

Classic sample

Through ABCDEDCBA.

n = 4

Shorter pyramid

Stops at ABCDCBA (Example 2).

n > 26

Past Z

Reject, clamp, or wrap — decide explicitly.

Bad input

Non-numeric scanf

Unchecked scanf fails silently — check the return value.

Case

Lowercase

Same loops with 'a' as the base.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Left half only

2. Spaces between letters

  • Print ch + " " in both halves
  • Keep the peak-once rule

3. Centered version

  • Add leading spaces (Example 3)
  • Reuse Program 16 padding ideas

4. Continue to Program 19

  • Mirrored alphabet with a shrinking gap
  • See Program 19

Notes

  • Peak once. Forward prints it; mirror starts one letter earlier.
  • Row length is 2r - 1 (1-based r); totals over n rows equal n².
  • Char arrays and direct char loops are interchangeable here.
  • Cap rows at 26 unless you intentionally leave A–Z.

Quick Takeaway: print up through the peak, mirror from peak minus one, then break the line — that is the whole pyramid.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Centered (Example 3)O(n²)O(1)

Row r (1-based) prints 2r - 1 letters; summing over n rows gives n² character writes.

Wrap Up

🎉 Conclusion

The palindromic alphabet pyramid is a small nested-loop exercise with lasting payoff: grow a peak, print forward, then mirror from peak - 1. Master the classic A–E sample, then try user input and optional centering.

Practice the three examples above, then continue to Program 19’s mirrored alphabet with a shrinking space gap.

Print A..peak, then (peak-1)..A, and break only after both halves finish.

💡 Best Practices

✅ Do

  • Start the mirror loop at peak - 1
  • Keep one consistent row indexing scheme
  • Cap rows at 26 for A–Z output
  • Check scanf for input
  • State O(n²) when asked about complexity

❌ Don’t

  • Start the reverse half at the peak letter
  • Mix 0-based and 1-based peaks casually
  • Call printf("\n") inside a letter loop
  • Ignore alphabet overflow on large n
  • Add centering before the palindrome is correct

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the palindromic alphabet pyramid the beginner-friendly way.

5
Core concepts
02

Forward

A..peak

Code
03

Mirror

peak-1..A

Code
04

New line

Ends each row

I/O
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

The peak letter was already printed by the first loop. Starting the mirror at peak-1 avoids printing the center letter twice.
Print A..peak, then print (peak-1)..A so the center letter is not duplicated.
Row r prints 2r-1 letters. Over n rows the total is 1+3+...+(2n-1)=n².
Yes. Use printf("%c ", j) in both halves, and update the sample output accordingly.
printf("%c", j) prints a letter and stays on the same line. printf("\n") ends the row after both halves finish.
Program 1 prints only the left half (A, AB, ABC…). This pattern mirrors back down so each full row is a palindrome.
O(n²) for n rows because each row prints O(n) characters and the sum of odd lengths is n².
Check scanf("%d", &rows) == 1, require n ≥ 1, and cap at 26 so peaks stay within A–Z.

Did you Know? 🔊

Each row prints A up to the row peak, then prints back down starting from peak - 1 so the middle letter appears only once. Row length is 2r - 1 for row r, so the total characters over n rows is .

Continue to Alphabet Pattern 19

Next up: mirrored alphabet rows with a shrinking space gap in the middle.

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