Palindromic Alphabet Pyramid in C

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

What You’ll Learn

Build each row as a palindrome by stitching a descending run (iB) with an ascending run (Ai). Compare with program 23, which right-aligns a different shape using %2c and spaces.

This version uses plain %c with no extra spacing.

⭐ Pattern Output

Five rows, peak letter 'A''E':

Output
A
BAB
CBABC
DCBABCD
EDCBABCDE
1

Complete C Program ('A''E')

Character literals; same logic as for (i = 65; i <= 69; i++) with j > 65 in the first inner loop.

c
#include <stdio.h>

int main() {
    int i, j;

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

    return 0;
}

🧠 How It Works

1

Outer i

Row peak letter: A, then B, … E.

Grow
2

Left wing j > 'A'

j starts at i and counts down, printing until it would hit A (exclusive), so you get B, CB, DCB, …

Desc
3

Right wing A..i

Always includes the center A and grows to AB, ABC, …

Asc
4

Palindrome check

Concatenation is symmetric around the single A from the second loop (e.g. B + AB = BAB).

Mirror
=

Length

Row i has 2 × (i - 'A') + 1 characters.

2

Variation — User Input

endChar = (char)('A' + rows - 1); same two inner loops with startChar instead of 'A' where needed.

c
#include <stdio.h>

int main() {
    int rows;
    int i, j;
    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 = i; j > startChar; --j) {
            printf("%c", j);
        }
        for (j = startChar; j <= i; ++j) {
            printf("%c", j);
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Center the pyramid with a leading-space loop from the widest row
  • Insert a space after each %c for an airy layout
  • Cap rows so endChar stays ≤ 'Z'

Avoid

  • Using j >= 'A' in the first loop without removing A from the second (double center)
  • Reversing loop order (forward then reverse) without adjusting bounds

Key Takeaways

1

The descending loop stops before A so the ascending loop owns the center.

2

Row length is the kth odd number 2k - 1 for the kth row from A.

3

Total characters for n rows: 1 + 3 + … + (2n - 1) = n² (e.g. 25 for five rows).

4

O(n²) time for n rows.

❓ Frequently Asked Questions

So A is printed only once by the forward loop. If the reverse loop included A, you would get AA at the seam.
Yes: the left part is the mirror of the right part except they share one center A when i > 'A'; for i = 'A' the string is just A.
Program 23 pads with spaces and uses %2c for a right-aligned block. Here there is no grid: only two letter loops per row.
O(n²) for n rows: total printed characters are Θ(n²).

Explore More C Alphabet Patterns!

Splitting a line into mirror halves with a shared center is a classic palindrome trick in console programs.

All Alphabet Patterns →
Did you know?

The first n odd positive integers sum to , which matches the total number of %c calls across n rows here.

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