Symmetric Alphabet Pyramid in C

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 A…E (output matches the loops below, including leading spaces from j from 69 down to i):
A
ABA
ABCBA
ABCDCBA
ABCDEDCBAComplete C Program ('A'–'E', ASCII)
Outer i from 65 to 69; n = k - 1 bridges the ascending and descending halves.
#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
Leading spaces
j runs from 'E' down to i; each step prints one space so the row shifts right as i grows.
Ascending half
k from 'A' through i prints the left side including the peak letter.
n = k - 1
When the second loop stops, k == i + 1. Subtracting one resets n to i so the first --n yields i - 1.
Mirror with --n
m runs i - 1 times (for i == 'A', the body is skipped). Each printf("%c", --n) walks down the alphabet.
Palindrome row
The center character is printed only in the ascending loop; the descending loop never revisits it.
Variation — User Input
endChar = (char)('A' + rows - 1); loops use startChar and endChar instead of literals.
#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 matchingcharbounds forj,k,m - Widen the pyramid with two spaces per
jstep - Try lowercase by shifting
startChar/endChar
Avoid
- Resetting
nwithout usingk—easy to be off by one on the mirror - Proportional fonts; use a monospace face to judge alignment
Key Takeaways
Three inner stages per row: pad, climb to i, step down with --n.
n = k - 1 ties the post-loop value of k to the mirror start.
Third loop count i - startChar matches how many letters sit left of the peak.
O(n²) work for n rows.
❓ Frequently Asked Questions
A through the current letter. The third loop prints --n for each position left of the peak, producing the reverse without repeating the center.for (k = 'A'; k <= i; k++), k is i + 1. So k - 1 is i, and the first --n prints i - 1.i, j, k, m as char (or keep them as int with 'A' bounds); the arithmetic stays the same.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.
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.
12 people found this page helpful
