Palindromic Alphabet Pyramid in C

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:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBAComplete 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.
#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
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.
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.
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.
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 A → ABA.
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.
Variation — User Input
endChar = 'A' + rows - 1 sets the bottom peak.
#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 <= 26for capital English letters
Avoid
- Second loop starting at
i(doubles the middle letter) - Using
unsigned charforkwheni == 'A'—i - 1can underflow badly
Key Takeaways
Ascending then descending halves build a palindrome.
k = i - 1 avoids printing the peak twice.
Total length for n rows: n² characters.
O(n²) time for n rows.
❓ Frequently Asked Questions
i = 'A', the second loop initializes k = i - 1, which is below 'A', so the condition k >= 'A' fails immediately.1 + 3 + … + (2n-1) = n².'a' and endChar in the lowercase range; the same loop structure applies.Explore More C Alphabet Patterns!
Splitting “up then down” is the standard way to code palindromes over a fixed alphabet slice.
When i = 'A' (65), k = i - 1 is 64, so k >= 'A' fails immediately — no extra if needed for the first row.
12 people found this page helpful
