Hollow Diamond Star Pattern in C

What You'll Learn
This pattern is Program 7 (inverted hollow V, top half of the diamond) followed by the same inner loops with Program 8’s downward i on the bottom half: after i runs 1…rows, run i from rows - 1 down to 1 with the same j and k inner loops and if (i == j) / if (i == k) tests.
Each printed line has width 2 * rows - 1 (for example 9 characters when rows = 5), including trailing spaces after the right star.
⭐ Pattern Output
When you run the program with rows = 5 (each line is 9 characters):
*
* *
* *
* *
* *
* *
* *
* *
* Complete C Program
Fixed rows = 5 version:
#include <stdio.h>
int main(void) {
int i, j, k;
int rows = 5;
/* Upper half: i = 1 .. rows */
for (i = 1; i <= rows; ++i) {
for (j = rows; j >= 1; --j) {
if (i == j) {
printf("*");
} else {
printf(" ");
}
}
for (k = 2; k <= rows; ++k) {
if (i == k) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
/* Lower half: avoid duplicate widest row */
for (i = rows - 1; i >= 1; --i) {
for (j = rows; j >= 1; --j) {
if (i == j) {
printf("*");
} else {
printf(" ");
}
}
for (k = 2; k <= rows; ++k) {
if (i == k) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}🧠 How It Works
Upper half (i = 1 … rows)
First for grows the hollow shape: i increases, so the two legs move apart row by row until i == rows (widest row, stars at both j == rows and k == rows).
Lower half (i = rows - 1 … 1)
Second for reuses the same j/k body but runs i downward from rows - 1, closing the V toward the bottom apex. Omitting i == rows here avoids printing the middle line twice.
Inner loops on every row
Each row: j scans rows … 1 (left diagonal); k scans 2 … rows (right diagonal). printf("*") or space from if (i == j) / if (i == k). Apex rows (i == 1) only get one star from the j side.
After every row
Both halves call printf("\n") at the end of each outer iteration. The inner block is duplicated in source so you can paste the same logic into two different i ranges without a function.
Full diamond
Line count: rows + (rows - 1) = 2 * rows - 1. Each line is 2 * rows - 1 characters wide — O(rows²) output, O(1) extra space. Middle rows scroll horizontally in the tutorial preview on phones.
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 = rows; j >= 1; --j) {
if (i == j) {
printf("*");
} else {
printf(" ");
}
}
for (k = 2; k <= rows; ++k) {
if (i == k) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
for (i = rows - 1; i >= 1; --i) {
for (j = rows; j >= 1; --j) {
if (i == j) {
printf("*");
} else {
printf(" ");
}
}
for (k = 2; k <= rows; ++k) {
if (i == k) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate
rows >= 1afterscanf - Replace
*with#, digits, or custom symbols - Print only the first outer loop for an inverted hollow V (Program 7)
- For a filled diamond, you need rules for the interior (for example distance from the vertical center), not only the two diagonal equalities
i == jandi == k - Parameterize spacing if you want a wider gap between the two legs
Avoid
- Starting the second outer loop at
i == rows—you would duplicate the widest row - Claiming
i != j && i != kalone builds a filled diamond (it does not; it inverts the hollow logic incorrectly) - Dropping trailing spaces so columns no longer align to width
2 * rows - 1
Key Takeaways
Upper half = Program 7; lower half = same inners with i counting down from rows - 1.
Widest row appears once, when i == rows in the first loop.
Each row: left edge via j, right edge via k; same tests on both halves.
Line width is always 2 * rows - 1 characters.
Time complexity O(n²) for n = rows.
❓ Frequently Asked Questions
i from 1 to rows (hollow inverted V: from the top apex to the wide equator). The second runs i from rows - 1 to 1 (hollow upright V, closing to the bottom apex). Both use the same j / k loops and diagonal conditions.i == rows in the first loop. Starting the second loop at rows - 1 prevents printing that line twice.n rows: about 2n - 1 lines, each with two inner passes of length Θ(n).Explore More C Star Patterns!
Programs 7 and 8 are the two halves of this diamond—practice them separately, then combine.
The hollow diamond is a direct composition: Program 7’s loop plus Program 8’s loop with the duplicate middle row removed by starting the second phase at rows - 1.
12 people found this page helpful
