V-Shaped Hollow Star Pattern in C

What You'll Learn
How to get an upright V from the same inner logic as Program 7 (inverted V): keep the j and k loops and if (i == j) / if (i == k) tests, but drive i from rows down to 1 so the wide row prints first and the legs close to the bottom vertex. Each line remains 2 * rows - 1 characters wide (9 when rows = 5).
On the last row (i == 1), only the j-loop prints the single vertex star; the k-loop prints spaces only.
⭐ 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;
for (i = rows; 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
Outer loop (reverse rows)
for (i = rows; i >= 1; --i) prints the widest row first (i == rows) and the single-star bottom row last (i == 1).
Left segment (j)
for (j = rows; j >= 1; --j): print * if i == j, else space. That draws the left leg of the V for every i.
Right segment (k)
for (k = 2; k <= rows; ++k) mirrors the rule on the right. When i == 1 (bottom vertex), this loop only prints spaces—the single star came from the j pass.
New line & width
printf("\n") after both segments. Line length is still 2 * rows - 1 characters; reversing the outer i order flips which row is widest.
Wide top, narrow bottom
Same j/k rules as the inverted V, but i counts down so the opening faces up. O(rows²) output, O(1) extra space; wide rows scroll in the preview on small viewports.
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 = rows; 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
- Switch the outer loop to
1..rowsto recover Program 7 (inverted V) - Validate
rows >= 1after input - Stack Program 7 (inverted V) then Program 8 (upright V), or see Program 9 for the full hollow diamond
- Print row index
iwhere stars appear
Avoid
- Claiming two stars on the bottom row from both loops—only
jhitsi == 1 - Incrementing
iin the outer loop (that produces Program 7’s inverted V, not this upright V) - Omitting spaces so columns no longer align
Key Takeaways
Same inner logic as Program 7 (inverted V); only outer loop direction changes to flip the shape upright.
Top row (i == rows): stars at j == rows and k == rows.
Bottom row (i == 1): one star from j; k loop is all spaces.
Line width remains 2 * rows - 1 characters.
Time complexity O(n²) for n rows.
❓ Frequently Asked Questions
i values run first, so the row with two outer stars (i == rows) is printed before smaller i values pull the legs inward.for (i = 1; i <= rows; ++i) (inverted V, narrow top). Program 8 uses for (i = rows; i >= 1; --i) (upright V, vertex at bottom). The j and k loops and the if conditions are the same.i == 1, the right loop only considers k >= 2, so i == k never holds. The bottom vertex is the lone * from j == 1, plus padding spaces to the right.n rows, same as Program 7.Explore More C Star Patterns!
Pair inverted V (Program 7) and upright V (Program 8) to build hollow diamonds and hourglass shapes.
This is the same trick as Program 6 versus Program 5: reverse the outer row index while keeping the per-row formulas unchanged.
12 people found this page helpful
