Diamond-Shaped Alphabet Pattern in C

What You’ll Learn
Stack the inverted V from program 33 on top of a flipped copy: nine rows total, widest at E, narrowest at A. The trick is running the bottom outer loop from D down to A so the middle row appears only once.
⭐ Pattern Output
Nine rows; each line is 9 characters wide (spaces included), matching the two inner loops (5 + 4 positions):
A
B B
C C
D D
E E
D D
C C
B B
A Complete C Program ('A'–'E')
Part one: i from 65 to 69. Part two: i from 68 down to 65. Inner loops are identical in both parts.
#include <stdio.h>
int main() {
int i, j, k;
// First part: A through E
for (i = 65; i <= 69; ++i) {
for (j = 69; j >= 65; --j) {
if (i == j) {
printf("%c", j);
} else {
printf(" ");
}
}
for (k = 66; k <= 69; ++k) {
if (i == k) {
printf("%c", k);
} else {
printf(" ");
}
}
printf("\n");
}
// Second part: D through A (avoid duplicate E row)
for (i = 68; i >= 65; --i) {
for (j = 69; j >= 65; --j) {
if (i == j) {
printf("%c", j);
} else {
printf(" ");
}
}
for (k = 66; k <= 69; ++k) {
if (i == k) {
printf("%c", k);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}🧠 How It Works
Top outer loop (grow the V)
i runs 65…69 (A…E). Each larger i places the letter farther out on both diagonals, so the hollow shape opens toward the widest E row.
Two inner passes per row
First, j scans E down to A; only when i == j do you print that letter, else a space—this draws the left/up leg. Then k scans B through E; only when i == k do you print—this draws the right/up leg. Together they place two letters on most rows (one letter on the first and last rows).
Bottom outer loop (shrink the V)
The second part runs i from 68 down to 65 (D…A) with the same inner loops pasted again. That rebuilds the arms in reverse so the diamond closes symmetrically.
Why the bottom starts at D
The top half already printed the single-wide E row. Continuing i from 68 skips repeating that middle line while still mirroring every narrower row.
Nine rows, two passes each
Five growing rows plus four shrinking rows; every row does two full scans over the letter ranges. That is O(n²) character decisions for n letters along each leg.
Variation — User Input (half height)
rows is the number of letters in the top half (e.g. 3 → A…C). Bottom loop runs endChar - 1 down to startChar.
#include <stdio.h>
int main() {
int rows;
int i, j, k;
char startChar, endChar;
printf("Enter the number of rows (half): ");
scanf("%d", &rows);
startChar = 'A';
endChar = (char)('A' + rows - 1);
for (i = startChar; i <= endChar; ++i) {
for (j = endChar; j >= startChar; --j) {
if (i == j) {
printf("%c", j);
} else {
printf(" ");
}
}
for (k = startChar + 1; k <= endChar; ++k) {
if (i == k) {
printf("%c", k);
} else {
printf(" ");
}
}
printf("\n");
}
for (i = endChar - 1; i >= startChar; --i) {
for (j = endChar; j >= startChar; --j) {
if (i == j) {
printf("%c", j);
} else {
printf(" ");
}
}
for (k = startChar + 1; k <= endChar; ++k) {
if (i == k) {
printf("%c", k);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Factor the inner two loops into a small function to remove duplication
- Fill the interior for a solid diamond of letters
- Use
charliterals for all bounds instead of ASCII numbers
Avoid
- Starting the bottom half at
endCharunless you intentionally want two center rows - Forgetting that
rowsin the variation means half of the diamond, not total line count
Key Takeaways
Two outer passes share the same j / k conditional printing.
Bottom pass begins at endChar - 1 to avoid duplicating the widest row.
Total rows = 2 × (endChar - startChar) + 1 for this layout.
O(n²) for half-height n letters.
❓ Frequently Asked Questions
i = A … E using the inverted-V logic. Part two prints i = D … A with the same inner loops, so the pattern narrows again symmetrically.'E' already ended part one. Beginning at 69 would draw that widest line twice.A…E version (nine rows, nine columns). For variable half-height n, work grows on the order of n²; do not confuse literal counts with the definition of big-O.A to the peak (each of about 2n - 1 rows does Theta(n) inner work).Continue With Number Patterns
The same two-part idea—grow then shrink—works for numeric diamonds too.
You could generate the bottom half by reusing the top loop with a separate data array, but duplicating the loop body keeps the symmetry obvious for learners.
12 people found this page helpful
