Hollow Diamond Inside Square Star Pattern in C

What You'll Learn
You print a frame with a solid row of * on the first line and again on the last line, and mirror the rows in between so the middle row is narrowest (only the left and right border stars with a wide hollow gap). The width is 2 * rows; the number of lines is 2 * rows - 1.
This is not the same loop structure as Program 9: there the diamond stands alone with constant width 2 * rows - 1 per line. Here the layout matches the classic “diamond inside a box” picture when rows = 5.
⭐ Pattern Output
When you run the program with rows = 5:
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********Complete C Program
Fixed rows = 5 version:
#include <stdio.h>
int main(void) {
int rows = 5;
int line, j;
int height = 2 * rows - 1;
int width = 2 * rows;
for (line = 1; line <= height; ++line) {
if (line == 1 || line == height) {
for (j = 1; j <= width; ++j) {
printf("*");
}
} else {
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
for (j = 1; j <= left; ++j) {
printf("*");
}
for (j = 1; j <= gap; ++j) {
printf(" ");
}
for (j = 1; j <= left; ++j) {
printf("*");
}
}
printf("\n");
}
return 0;
}🧠 How It Works
Grid size
width = 2 * rows, height = 2 * rows - 1. For rows = 5 you get 10 columns and 9 lines.
Top and bottom bars
When line == 1 or line == height, print width stars. Those are the full horizontal edges of the frame.
Inner rows
Map line to i (distance from the tip): i = line when line <= rows, else i = 2 * rows - line. Then left = rows - i + 1, gap = 2 * (i - 1). Three inner passes: left stars, gap spaces, left stars.
Finish each line
printf("\n") runs after the full-width bars and after each inner triple, so every output row is exactly width = 2 * rows characters (10 when rows = 5).
Hollow center
The middle gap widens to the waist (line == rows), then mirrored lines tighten it. O(rows²) character writes, O(1) extra memory; 10-column lines scroll horizontally in the preview on phones.
Variation — User Input Version
Use scanf for rows:
#include <stdio.h>
int main(void) {
int rows;
int line, j;
int height, width;
printf("Enter the number of rows: ");
scanf("%d", &rows);
height = 2 * rows - 1;
width = 2 * rows;
for (line = 1; line <= height; ++line) {
if (line == 1 || line == height) {
for (j = 1; j <= width; ++j) {
printf("*");
}
} else {
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
for (j = 1; j <= left; ++j) {
printf("*");
}
for (j = 1; j <= gap; ++j) {
printf(" ");
}
for (j = 1; j <= left; ++j) {
printf("*");
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Reject
rows < 1after input - Rewrite the inner part as one loop over
j = 1…widthwith boolean tests (border OR left block OR right block) - Compare side-by-side with Program 9 output for the same
rows - Swap
*for digits on the border only
Avoid
- Mixing up
2 * rows - 1(line count) with2 * rows(line width)—both appear in this pattern - Copy-pasting
i == j/i + j == rows + 1snippets withjonly runningi…rows; that does not reproduce this 10-wide picture forrows = 5 - Trailing extra space loops after the right star block (they break alignment)
Key Takeaways
Width 2n, height 2n - 1 for n = rows.
First and last lines: solid bars of *.
Other lines: left stars, gap spaces, left stars with i mirrored.
Not the same as Program 9’s diagonal-only loop pattern.
Time complexity O(n²).
❓ Frequently Asked Questions
left and gap from the formulas above, with i mirrored below the waist row.2 * rows, so 10 when rows = 5. The height is 2 * rows - 1 (9 lines), so the outer shape uses a wide horizontal bar and matching side columns on the inner rows.2 * rows - 1. This program adds full-width top and bottom rows of length 2 * rows and builds each inner line from left block, gap, and right block.n = rows: Θ(n) lines, each printing Θ(n) characters.You’ve Reached the Last Numbered Star Pattern
Review Programs 9 and 10, then browse the hub for more C topics.
Many older tutorials show a different C listing for this picture; the version here is checked so every line length matches the math: 2 * left + gap == 2 * rows on inner rows.
12 people found this page helpful
