Diamond Alphabet & Stars in C

What You’ll Learn
Build a vertical diamond where each horizontal line is the same letter separated by *, widening to the middle then narrowing again.
Compare C star diamonds (geometry only) and program 15 (mirror with a star block).
⭐ Pattern Output
Half height 5 (ASCII assumed for 'A' + offset):
A
B*B
C*C*C
D*D*D*D
E*E*E*E*E
D*D*D*D
C*C*C
B*B
AComplete C Program (Half Height 5)
i + 64 matches 'A' + i - 1 on ASCII. Prefer ch = (char)('A' + i - 1) for clarity.
#include <stdio.h>
int main() {
int i, j;
char ch;
for (i = 1; i <= 5; ++i) {
ch = (char)('A' + i - 1);
for (j = 1; j < i * 2; ++j) {
if (j % 2 == 0) {
printf("*");
} else {
printf("%c", ch);
}
}
printf("\n");
}
for (i = 4; i >= 1; --i) {
ch = (char)('A' + i - 1);
for (j = 1; j < i * 2; ++j) {
if (j % 2 == 0) {
printf("*");
} else {
printf("%c", ch);
}
}
printf("\n");
}
return 0;
}🧠 How It Works
Upper half: i = 1 .. n
Each row gets its own row letter: ch = (char)('A' + i - 1). Row 1 is all A, row 2 alternates B and *, and so on until the middle row uses nth letter.
Inner stripe: j from 1 to 2i - 1
for (j = 1; j < i * 2; ++j) runs an odd number of positions. Even j prints *; odd j prints ch. That yields B*B, C*C*C, … because stars sit between repeated row letters.
Lower half: i = n-1 .. 1
After the peak row at i = n, a second outer loop counts i down to 1 with the same inner logic. That mirrors the upper half without printing the widest line twice.
Why width is 2i - 1
For row index i you need i letters and i - 1 gaps between them, which is 2i - 1 symbols. At n = 5 the middle line has nine tokens: E*E*E*E*E.
Diamond cost
Upper and lower halves each sum 1 + 3 + … + (2n-1) = n² cells, minus one shared middle row when you split loops as in the sample. Overall print volume is Θ(n²) for half-height n.
Variation — User Input (Half Height)
Lower loop i = rows - 1 down to 1.
#include <stdio.h>
int main() {
int rows, i, j;
char ch;
printf("Enter the number of rows (half): ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
ch = (char)('A' + i - 1);
for (j = 1; j < i * 2; ++j) {
if (j % 2 == 0) {
printf("*");
} else {
printf("%c", ch);
}
}
printf("\n");
}
for (i = rows - 1; i >= 1; --i) {
ch = (char)('A' + i - 1);
for (j = 1; j < i * 2; ++j) {
if (j % 2 == 0) {
printf("*");
} else {
printf("%c", ch);
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Add leading spaces so the diamond is centered in the terminal
- Swap
*for-or another separator - Cap
rowsso'A' + rows - 1 <= 'Z'
Avoid
- Second loop from
rowsinstead ofrows - 1(duplicates the widest row) - Using
if (j % 2)with the same branches as this program without swapping — parity flips
Key Takeaways
Odd j → letter, even j → * (with this loop layout).
j < 2*i gives 2i - 1 symbols per row.
Mirror with i = n-1 .. 1 after 1 .. n.
O(n²) prints for half-height n.
❓ Frequently Asked Questions
j prints *; odd j prints the row letter. If you flipped the branches, you would flip the pattern.A); the lower loop starts at 0 and does nothing.'A' + i - 1 states the intent more clearly.Explore More C Alphabet Patterns!
Two-phase loops (up then down) are the usual way to code diamonds without repeating the center line.
On row i the letter appears i times and * appears i - 1 times, for a total of 2i - 1 characters.
12 people found this page helpful
