Shape Rule
Vertical diamond
Widen 1..n, then mirror n-1..1 without duplicating the middle.

Build a vertical diamond where each row repeats the same letter, with * between letters. The pattern widens to the middle row, then mirrors back down — A, B*B, C*C*C, …, E*E*E*E*E, then back to A. Compare Program 15 (stars in the center) and Program 19 (mirrored letters with spaces). Includes a live preview, worked C examples, edge cases, and complexity.
Vertical diamond
Widen 1..n, then mirror n-1..1 without duplicating the middle.
One per row
Row i uses letter 'A' + i - 1 (or alpha[i-1]).
2i − 1
Inner loop prints 1, 3, 5, … characters per row.
j % 2
Odd positions print the letter; even positions print *.
Half height
Pick half height 1–8 and draw the diamond instantly.
Complexity
Odd-length rows up and down sum to O(n²).
A diamond alphabet pattern with stars repeats one letter on each row and places * between those letters, widening to a middle row and then mirroring back down.
In C you usually solve it with two outer loops (upper and lower halves) and an inner loop that uses j % 2 to choose letter vs star.
It teaches three classic ideas at once: odd row lengths, position-based alternation, and mirroring without duplicating the widest row.
Grow to n, then mirror from n-1.
Rows have 1, 3, 5, … characters.
Letter on odd, * on even.
Row i repeats letter number i.
In short: for each half-height row, print 2i-1 characters alternating letter and *, then mirror from n-1 down to 1.
Given a half height n (or fixed 5), print a vertical diamond of alternating letters and stars.
// Half height 5
// A
// B*B
// C*C*C
// D*D*D*D
// E*E*E*E*E
// D*D*D*D
// C*C*C
// B*B
// A | Item | Type | Description |
|---|---|---|
n | int | Half height (middle row letter = ‘A’ + n − 1). Cap at 26 for A–Z. |
| Printed output | text | About 2n-1 rows of letter/* patterns forming a diamond. |
for i in 1..n:
ch = 'A' + i - 1
for j in 1..(2i-1):
print '*' if j even else ch
print newline
for i in (n-1)..1:
(same inner loop) | Approach | Idea | Best for |
|---|---|---|
| Two outer halves | 1..n then n-1..1 | Matching this classic sample |
| Helper method | Extract “print row i” once | Avoiding duplicated inner loops |
| Goal | Pattern |
|---|---|
| Upper half | for (int i = 1; i <= n; i++) |
| Lower half | for (int i = n - 1; i >= 1; i--) |
| Row length | for (int j = 1; j < i * 2; j++) → 2i-1 chars |
| Alternate | if (j % 2 == 0) printf("*"); else printf("%c", ch); |
| Row letter | char ch = (char)('A' + i - 1); |
| End the row | printf("\n"); |
Same row — different roles by column index.
letterPrints the current row letter (A, B, C…)
*Prints the separator between letters
widthOdd length so the row ends on a letter
breakEnds the row after the alternating run
Reach for this when teaching vertical mirrors and position-based alternation.
You already know half-and-mirror; now alternate symbols inside each row.
Practice j % 2 for clean letter/star placement.
Same 1, 3, 5… idea used in many pyramids and diamonds.
Replace * with - or spaces for variant labs.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one modulo check plus a careful lower-half start builds a clean vertical diamond.
Choose a half height between 1 and 8 and draw the diamond alphabet-and-stars pattern in the browser.
Three complete C programs — fixed half height 5, scanf half height, and a shared print_row helper. Click View Output to reveal sample console results.
Print the classic diamond with a char array and two halves.
5Odd j prints the row letter; even j prints *. Upper half prints 1..5, lower half prints 4..1.
#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;
} When i = 3, the inner loop runs j = 1..5 and prints C * C * C. The lower half starts at 4 so E*E*E*E*E appears only once.
Let the user choose the half height.
Uses a computed row letter ch = 'A' + i - 1. Check scanf in real apps.
#include <stdio.h>
int main() {
int n, i, j;
char ch;
printf("Enter half height (like 5): ");
scanf("%d", &n);
for (i = 1; i <= n; ++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 = n - 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;
} Same alternation and mirror rules; only n changes the size. Cap n at 26 so middle-row letters stay within A–Z.
Extract the row printer so upper and lower halves share one loop body.
Same diamond, less duplicated code.
#include <stdio.h>
void print_row(int i) {
int j;
char ch = (char)('A' + i - 1);
for (j = 1; j < i * 2; ++j) {
if (j % 2 == 0) {
printf("*");
} else {
printf("%c", ch);
}
}
printf("\n");
}
int main() {
int n = 5;
int i;
for (i = 1; i <= n; ++i) {
print_row(i);
}
for (i = n - 1; i >= 1; --i) {
print_row(i);
}
return 0;
} print_row(i) owns the letter/* alternation. The two outer loops only decide which row heights to print.
The first outer loop runs i = 1..n, making the row length grow.
Inner loop prints 2i-1 characters: odd j prints the letter, even j prints *.
Second outer loop runs i = n-1..1 so the widest row is not duplicated.
printf("\n") ends each row after the alternating run.
Total printed characters scale like O(n²) for half height n.
Trace each half and the characters printed on each row.
| Half | i | Letter | Chars (2i−1) | Printed row |
|---|---|---|---|---|
| Upper | 1 | A | 1 | A |
| Upper | 2 | B | 3 | B*B |
| Upper | 3 | C | 5 | C*C*C |
| Lower | 2 | B | 3 | B*B |
| Lower | 1 | A | 1 | A |
Total rows: 2n - 1 = 5. Middle row C*C*C appears once.
Where this diamond letter/star idea shows up beyond the homework prompt.
Clearest alphabet demo of j % 2 choosing two symbols.
Example: swap * for - and compare.
Practice starting the lower half at n-1.
Example: start at n once and see the doubled middle.
Refactor duplicated halves into print_row (Example 3).
Example: one method, two calling loops.
Add leading spaces later for a true 2D diamond silhouette.
Example: pad with n - i spaces before each row.
Odd sums up and down make O(n²) easy to see.
Example: n=5 prints 25 + 16 = 41 characters.
Practice limiting half height so letters stay in A–Z.
Example: reject n > 26 or clamp it.
Pro Tip: say “odd letter, even star, mirror from n minus one” before coding — that story prevents a doubled middle row.
Why this pattern earns a spot among diamond and separator labs.
Wrong modulo or a duplicated middle row shows up immediately.
One j % 2 check drives the whole letter/star effect.
A small helper removes duplicated upper/lower inner loops.
Streaming output needs no storage beyond loop variables.
Pro Tip: get the upper half right first; only then add the lower half starting at n-1.
Small habits that keep diamond letter/star code clean.
That single off-by-one avoids duplicating the widest row.
Use 2i-1 so every row ends on a letter, not a star.
scanfAvoid undefined behavior when the user types letters instead of a number.
Beyond Z you need a wrap/stop policy for row letters.
print_rowShare one inner loop between upper and lower halves.
Pro Tip: if the middle letter row appears twice, you almost certainly started the lower half at n instead of n-1.
Mistakes that commonly break diamond alphabet-and-star patterns.
Duplicates the widest row in the middle.
→ Start from n - 1.
Ending on a star breaks the letter-star-letter rhythm.
→ Print exactly 2i - 1 characters.
Printing stars on odd positions yields *B* instead of B*B.
→ Letter on odd j, star on even j.
scanfLetters or empty input leave n uninitialized.
→ Check scanf’s return value and re-prompt on failure.
Large half heights walk past the alphabet.
→ Cap n at 26 or define a wrap policy.
Check these inputs before calling the solution done.
Output is just A; lower half does not run.
Middle row is E*E*E*E*E.
Five rows through C*C*C.
Reject, clamp, or wrap — decide explicitly.
Unchecked scanf fails silently — check the return value.
Same loops; only the even-position character changes.
Try these variations to lock in the pattern.
- instead of *n - i leading spacesprint_rown-1 so the widest row is not repeated.2i-1) so rows end on a letter.j → letter; even j → *.2n - 1 for half height n.Quick Takeaway: print odd-length letter/star rows from 1 to n, then mirror from n-1 to 1 — that is the whole diamond.
| Program | Time | Extra space |
|---|---|---|
| Fixed / input (Examples 1–2) | O(n²) | O(1) |
| Helper method (Example 3) | O(n²) | O(1) |
Upper half prints about n² characters (sum of odds); lower half adds almost the same without the middle row — still O(n²).
The diamond alphabet-and-stars pattern is a small nested-loop exercise with lasting payoff: odd row lengths, position-based alternation, and a careful vertical mirror. Master the classic A…E…A sample, then try user input and a helper-method rewrite.
Practice the three examples above, then continue to Program 22’s right-aligned sequential alphabet pyramid.
Print 2i-1 characters with letter on odd positions and * on even ones, grow to n, then mirror from n-1.
n - 12i - 1)j, stars on even jscanf and cap at 26n (duplicates middle)printf("\n") inside the alternating loopPrint the diamond alphabet-and-stars pattern the beginner-friendly way.
Widen then mirror
Definitionj % 2 letter/*
Code2i − 1 chars
CodeLower starts here
I/OO(n²) time
AnalysisUpper half prints rows 1..n; lower half prints n-1..1 so the widest row appears once. Each row runs j = 1..(2i-1). Odd j prints the row letter, even j prints *.
Next up: sequential letter pyramids with nested loops.
12 people found this page helpful