Filled Diamond Star Pattern in C

What You'll Learn
You combine a center-aligned pyramid (Program 5) with its mirror: same inner loops—(rows - i) spaces, then (2 * i - 1) stars—but after i reaches rows, run i from rows - 1 down to 1 (the same outer-loop idea as Program 6).
Unlike Program 9 (hollow diamond), rows are not padded to a fixed width: the tip row is shorter; the middle row has 2 * rows - 1 stars and no leading spaces when i == rows.
⭐ Pattern Output
When you run the program with rows = 5:
*
***
*****
*******
*********
*******
*****
***
*Complete C Program
Fixed rows = 5 version (same j / k inner loops as Program 5):
#include <stdio.h>
int main(void) {
int rows = 5;
int i, j, k;
/* Upper half */
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= rows - i; ++j) {
printf(" ");
}
for (k = 1; k <= 2 * i - 1; ++k) {
printf("*");
}
printf("\n");
}
/* Lower half (no duplicate widest row) */
for (i = rows - 1; i >= 1; --i) {
for (j = 1; j <= rows - i; ++j) {
printf(" ");
}
for (k = 1; k <= 2 * i - 1; ++k) {
printf("*");
}
printf("\n");
}
return 0;
}🧠 How It Works
Upper half (i = 1 … rows)
for (j = 1; j <= rows - i; ++j) printf(" "); then for (k = 1; k <= 2 * i - 1; ++k) printf("*"); — centered pyramid growing to the widest row.
Lower half (i = rows - 1 … 1)
Same two inner loops; outer i steps down so odd widths shrink (e.g. 7, 5, 3, 1 when rows = 5). Starting at rows - 1 avoids duplicating the widest row.
Why 2 * i - 1?
Values 1, 3, 5, …, 2*rows-1 add one * on each side as i grows, so the solid block stays symmetric around the center column.
New line each row
printf("\n") closes every row in both halves. Total lines = 2*rows - 1; each line length is still (rows-i)+(2i-1)=rows+i-1.
Solid diamond
2*rows - 1 lines, each up to 2*rows - 1 characters wide — O(rows²) output, O(1) extra space. Widest rows scroll inside the green glyph on small screens.
Variation — User Input Version
Use scanf for rows:
#include <stdio.h>
int main(void) {
int rows;
int i, j, k;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= rows - i; ++j) {
printf(" ");
}
for (k = 1; k <= 2 * i - 1; ++k) {
printf("*");
}
printf("\n");
}
for (i = rows - 1; i >= 1; --i) {
for (j = 1; j <= rows - i; ++j) {
printf(" ");
}
for (k = 1; k <= 2 * i - 1; ++k) {
printf("*");
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate
rows >= 1afterscanf - For a hollow diamond, use Program 9 (diagonal
j/klogic)—not a one-line tweak of the star loop - Print only the first outer loop to get Program 5’s pyramid alone
- Swap characters or print row numbers inside the star run
- Insert spaces between
*characters for a looser diamond
Avoid
- Starting the second outer loop at
i == rows—duplicate widest row - Confusing this with Program 9: hollow diamonds need fixed width
2 * rows - 1per line and different inner logic - Using
istars instead of2 * i - 1—breaks centered symmetry
Key Takeaways
Upper half = Program 5; lower half = same inners with i from rows - 1 to 1.
Stars per row: 2 * i - 1; leading spaces: rows - i.
Widest row has 2 * rows - 1 stars; tip rows are shorter (no fixed line width).
Total lines: 2 * rows - 1.
Time complexity O(n²) for n = rows.
❓ Frequently Asked Questions
i and prints wider star runs each time. The second loop shrinks i and reuses the same space and star formulas, closing the diamond.2 * rows - 1 stars is already printed when i == rows in the first loop. Continuing from rows - 1 avoids printing that line twice.*. Program 9 only places stars on two diagonals and pads each line to length 2 * rows - 1.n rows: about 2n - 1 lines, each with Θ(n) printing work in the two inner loops.Explore More C Star Patterns!
Master Program 5 and Program 6, then this diamond is just both halves back-to-back.
The filled diamond is the same nested-loop body as Programs 5 and 6; only the sequence of i values changes: up through rows, then down from rows - 1 without repeating the peak row.
12 people found this page helpful
