Centered Alphabet Pyramid in C

What You’ll Learn
Three rows, odd-width letter groups, padded with spaces so the block lines up like a pyramid. Letters flow in order through one counter k.
Related: program 14 (odd-width triangle without centering) and star pyramids (spaces + symbols).
⭐ Pattern Output
Three rows (spaces between letters; leading spaces align columns):
A
B C D
E F G H IComplete C Program (Three Rows)
Bottom row ends at 'E' (69); outer i runs 65, 67, 69. Inner j runs 69 down to 65.
#include <stdio.h>
int main() {
int i, j, k;
k = 65;
for (i = 65; i <= 69; i += 2) {
for (j = 69; j >= 65; --j) {
if (j > i) {
printf(" ");
} else {
printf("%c ", k++);
}
}
printf("\n");
}
return 0;
}🧠 How It Works
k = 65 streams letters
Like other “flood” triangles, k never resets. Each real letter print uses k++, so the pattern reads as one continuous A, B, C, … sequence dropped into a grid.
Outer i += 2 picks odd caps
The row anchor jumps 65 → 67 → 69 (A, C, E). That makes each row wider by two letter slots while keeping the cap on odd letters only.
Inner j walks the full top row width
j counts from 69 down to 65 every time. When j > i you print a space; otherwise you print the next streamed letter with printf("%c", k++). Fixed-width scanning keeps right alignment.
First row detail
For i = 65, j = 69,68,67,66 are all > i (four spaces); j = 65 prints A.
Inner work per row
This sample always runs five inner steps; for general rows the inner width is 2 * rows - 1 positions per line, so total work is O(r²).
Variation — User Input
Last outer i is 65 + 2*(rows-1); inner j starts at the same value. Same j > i rule.
#include <stdio.h>
int main() {
int rows, i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &rows);
k = 65;
for (i = 65; i <= 65 + 2 * (rows - 1); i += 2) {
for (j = 65 + 2 * (rows - 1); j >= 65; --j) {
if (j > i) {
printf(" ");
} else {
printf("%c ", k++);
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Rewrite with
for (i = 'A'; i <= 'E'; i += 2)and matchingjbounds for readability - Two-loop style: print
(last - i) / 2spaces, then a dedicated letter loop (same geometry, clearer intent) - Clamp
rowssokdoes not pass'Z'
Avoid
- Confusing
j > iwith odd-onlyj— every integerjbetween bounds is visited - Resetting
keach row (you would repeatAevery line)
Key Takeaways
Fixed-width inner scan + j > i produces leading padding.
k++ only on printed letters keeps the alphabet continuous.
Outer step 2 gives 1, 3, 5, … letters per row.
O(r²) for r rows with width scaling with r.
❓ Frequently Asked Questions
i = 65, values j = 69, 68, 67, 66 satisfy j > i, so you print four spaces; j = 65 prints the letter.65 + 2*(rows-1) is the ASCII code of the last row’s end letter when rows is 3 that is 69 ('E').printf("%c", k++). Centering logic is unchanged; only the letter spacing tightens.Explore More C Alphabet Patterns!
Centering is often “print padding, then print the payload” — here both share one inner loop.
When tracing row 1, list every j from 69 down to 66 — each is > 65, so you get four padding spaces, not only two values.
12 people found this page helpful
