Palindromic Alphabet Pyramid in C

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

What You’ll Learn

Each row is a palindrome built from the alphabet: climb from A to the row letter, then step back down without repeating the peak.

Contrast program 17 (reverse line with a diagonal *) and program 1 (only the left half).

⭐ Pattern Output

Five rows, no spaces between letters:

Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
1

Complete C Program (Through 'E')

ASCII form: i from 65 to 69; j from 65 to i; k from i - 1 to 65. When i == 'A', the second loop starts at '@' and does not run.

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

1

Outer i as the peak letter

i walks A through E. Whatever i is on this row becomes the highest letter printed on that line; shorter rows are built from the same pattern with a smaller peak.

Rows
2

First inner loop: j from 'A' to i

This prints the ascending run including the center letter. For i = 'C' you emit ABC. The loop variable is the character code, so printf("%c", j) is enough.

Up
3

Second inner loop: k from i - 1 down to 'A'

Starting at i - 1 avoids printing the peak twice. Counting down rebuilds the left side in reverse: for i = 'C' you append BA after ABC to get ABCBA.

Down
4

First row edge case

When i = 'A', the first loop prints only A. The second loop range is empty (i - 1 is below 'A'), so the row stays a single letter. For i = 'B' you get AB then AABA.

Check
=

Odd-length rows

Row r (1-based) prints 2r - 1 characters. For five peaks that is 1+3+5+7+9 = 25 = 5². Two inner passes per outer row give O(n²) work for n letters.

2

Variation — User Input

endChar = 'A' + rows - 1 sets the bottom peak.

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;
}

💡 Tips for Enhancement

Try These

  • Add leading spaces to center each row like a pyramid on screen
  • Use printf("%c ", j) for spaced-out letters
  • Keep rows <= 26 for capital English letters

Avoid

  • Second loop starting at i (doubles the middle letter)
  • Using unsigned char for k when i == 'A'i - 1 can underflow badly

Key Takeaways

1

Ascending then descending halves build a palindrome.

2

k = i - 1 avoids printing the peak twice.

3

Total length for n rows: n² characters.

4

O(n²) time for n rows.

❓ Frequently Asked Questions

For i = 'A', the second loop initializes k = i - 1, which is below 'A', so the condition k >= 'A' fails immediately.
Yes. Row lengths are 1, 3, 5, 7, 9; their sum is 25 = 5². In general, 1 + 3 + … + (2n-1) = n².
Use 'a' and endChar in the lowercase range; the same loop structure applies.
O(n²) for n rows.

Explore More C Alphabet Patterns!

Splitting “up then down” is the standard way to code palindromes over a fixed alphabet slice.

All Alphabet Patterns →
Did you know?

When i = 'A' (65), k = i - 1 is 64, so k >= 'A' fails immediately — no extra if needed for the first row.

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