Inverted V-Shaped Hollow Star Pattern in C

What You'll Learn
How to build a hollow inverted V: one star on the first row, then the two legs move outward row by row until the base row shows stars at both sides. Two inner loops per row print * or space from i == j and i == k. The example uses rows = 5; each line has 2 * rows - 1 characters (here, 9).
Compare Program 8 for the upright V (wide top, narrow bottom). Together they form the top and bottom halves of a hollow diamond in Program 9.
⭐ Pattern Output
When you run the program with rows = 5, you’ll see (each line is 9 characters wide):
*
* *
* *
* *
* *Complete C Program
Fixed rows = 5 version:
#include <stdio.h>
int main(void) {
int i, j, k;
int rows = 5;
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");
}
return 0;
}🧠 How It Works
Outer Loop (Rows)
for (i = 1; i <= rows; ++i) walks from the narrow top row (i == 1, single star) down to the wide base (i == rows, two outer stars).
Left segment (j)
for (j = rows; j >= 1; --j): printf("*") when i == j, else printf(" "). That draws the left leg of the widening V.
Right segment (k)
for (k = 2; k <= rows; ++k): printf("*") when i == k, else space. Starting at 2 avoids a second star on row 1 (the apex already came from the j pass).
New line & width
printf("\n") ends each row. The j loop runs rows times and the k loop rows - 1 times, so every line has 2 * rows - 1 columns (9 when rows = 5).
Hollow between the legs
Stars only on the two diagonals (i == j and i == k); everything else is a space. O(rows²) character writes, O(1) extra memory. Full-width rows scroll sideways in the green preview on narrow screens.
Variation — User Input Version
Replace the constant with scanf (same logic, rows instead of 5):
#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");
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate
rows >= 1after input - Parameterize both loops with
rowsand tracei == j/i == kon paper - Pair with Program 8 (upright V) to sketch a full hollow diamond
- Print row numbers at star positions instead of
* - Generalize column indices if you want a wider gap between the legs
Avoid
- Starting the second loop at
k = 1without adjusting logic (duplicate*on row 1) - Mixing up
jdescending vs ascending—the pattern assumesjgoesrows → 1 - Forgetting spaces so columns collapse
- Assuming
scanfalways succeeds
Key Takeaways
Left leg: loop j from rows to 1; star when i == j.
Right leg: loop k from 2 to rows; star when i == k.
Line width is rows + (rows - 1) = 2 * rows - 1 characters.
Time complexity O(n²) for n rows.
Often taught as the top half of a hollow diamond.
❓ Frequently Asked Questions
* per row, exactly where the row index matches the column index being visited. Everything else is a space, so you only see the two slanted edges.i == j hit lands on the correct column for each row, forming the left arm of the inverted V.k started at 1, i == k would fire again and duplicate it.n rows: each row does Θ(n) iterations across the two inner loops.Explore More C Star Patterns!
Hollow diamonds and banners reuse the same idea: decide star vs space with row and column indices.
The same inverted V can be drawn with different loop orders; this version is compact because each row places at most two stars using simple i == j and i == k tests.
12 people found this page helpful
