Symmetric Alphabet Pyramid in C

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

What You’ll Learn

Build a centered pyramid where each row reads the same forwards and backwards: A, ABA, ABCBA, and so on. Spaces right-align the row; the tricky part is reusing k’s value after the ascending loop and printing --n for the descending half.

Compare with program 31 (V shape with mostly blanks) and plain triangles earlier in the series.

⭐ Pattern Output

Five rows for AE (output matches the loops below, including leading spaces from j from 69 down to i):

Output
     A
    ABA
   ABCBA
  ABCDCBA
 ABCDEDCBA
1

Complete C Program ('A''E', ASCII)

Outer i from 65 to 69; n = k - 1 bridges the ascending and descending halves.

c
#include <stdio.h>

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

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

    return 0;
}

🧠 How It Works

1

Leading spaces

j runs from 'E' down to i; each step prints one space so the row shifts right as i grows.

j
2

Ascending half

k from 'A' through i prints the left side including the peak letter.

k
3

n = k - 1

When the second loop stops, k == i + 1. Subtracting one resets n to i so the first --n yields i - 1.

n
4

Mirror with --n

m runs i - 1 times (for i == 'A', the body is skipped). Each printf("%c", --n) walks down the alphabet.

m
=

Palindrome row

The center character is printed only in the ascending loop; the descending loop never revisits it.

2

Variation — User Input

endChar = (char)('A' + rows - 1); loops use startChar and endChar instead of literals.

c
#include <stdio.h>

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Rewrite with for (i = 'A'; i <= 'E'; ++i) and matching char bounds for j, k, m
  • Widen the pyramid with two spaces per j step
  • Try lowercase by shifting startChar / endChar

Avoid

  • Resetting n without using k—easy to be off by one on the mirror
  • Proportional fonts; use a monospace face to judge alignment

Key Takeaways

1

Three inner stages per row: pad, climb to i, step down with --n.

2

n = k - 1 ties the post-loop value of k to the mirror start.

3

Third loop count i - startChar matches how many letters sit left of the peak.

4

O(n²) work for n rows.

❓ Frequently Asked Questions

Spaces align the row. The second loop prints A through the current letter. The third loop prints --n for each position left of the peak, producing the reverse without repeating the center.
After for (k = 'A'; k <= i; k++), k is i + 1. So k - 1 is i, and the first --n prints i - 1.
Yes. Use i, j, k, m as char (or keep them as int with 'A' bounds); the arithmetic stays the same.
O(n²) for n rows because each row spends Theta(n) time across the inner loops.

Keep Going With Alphabet Patterns!

Symmetric rows are a small step from triangles: add a mirror pass and one integer bridge variable.

All Alphabet Patterns →
Did you know?

For i == 'A', the condition m < i is false immediately, so the third loop prints nothing—the first row is a single letter plus padding.

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