Palindromic Alphabet Pyramid in C

What You’ll Learn
Build each row as a palindrome by stitching a descending run (i ↓ B) with an ascending run (A ↑ i). 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':
A
BAB
CBABC
DCBABCD
EDCBABCDEComplete C Program ('A'–'E')
Character literals; same logic as for (i = 65; i <= 69; i++) with j > 65 in the first inner loop.
#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
Outer i
Row peak letter: A, then B, … E.
Left wing j > 'A'
j starts at i and counts down, printing until it would hit A (exclusive), so you get B, CB, DCB, …
Right wing A..i
Always includes the center A and grows to AB, ABC, …
Palindrome check
Concatenation is symmetric around the single A from the second loop (e.g. B + AB = BAB).
Length
Row i has 2 × (i - 'A') + 1 characters.
Variation — User Input
endChar = (char)('A' + rows - 1); same two inner loops with startChar instead of 'A' where needed.
#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
%cfor an airy layout - Cap
rowssoendCharstays ≤'Z'
Avoid
- Using
j >= 'A'in the first loop without removingAfrom the second (double center) - Reversing loop order (forward then reverse) without adjusting bounds
Key Takeaways
The descending loop stops before A so the ascending loop owns the center.
Row length is the kth odd number 2k - 1 for the kth row from A.
Total characters for n rows: 1 + 3 + … + (2n - 1) = n² (e.g. 25 for five rows).
O(n²) time for n rows.
❓ Frequently Asked Questions
A is printed only once by the forward loop. If the reverse loop included A, you would get AA at the seam.A when i > 'A'; for i = 'A' the string is just A.%2c for a right-aligned block. Here there is no grid: only two letter loops per row.Θ(n²).Explore More C Alphabet Patterns!
Splitting a line into mirror halves with a shared center is a classic palindrome trick in console programs.
The first n odd positive integers sum to n², which matches the total number of %c calls across n rows here.
12 people found this page helpful
