V-Shaped Hollow Star Pattern in C++

What You'll Learn
This program prints a hollow V (two stars on the first row, then closer together each row until one star on the last row). See Program 7 for the inverted V (narrow top, wide base). This page’s pattern is the lower half of the hollow diamond (Program 9).
Each line has width 2 * rows - 1 (9 characters when rows = 5), including spaces used for alignment.
⭐ Pattern Output
When you run the program with rows = 5:
* *
* *
* *
* *
* Complete C++ Program
Fixed rows = 5 version:
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j, k;
for (i = rows; i >= 1; --i) {
for (j = rows; j >= 1; --j) {
if (i == j) cout << "*";
else cout << " ";
}
for (k = 2; k <= rows; ++k) {
if (i == k) cout << "*";
else cout << " ";
}
cout << "\n";
}
return 0;
}🧠 How It Works
Outer loop: wide row first
for (i = rows; i >= 1; --i) prints the opening row with two outer stars, then closes toward the bottom vertex. Same j/k structure as the inverted V, reversed i order.
Left leg (j)
for (j = rows; j >= 1; --j) with if (i == j) cout << "*"; else cout << " "; draws the left diagonal.
Right leg (k)
for (k = 2; k <= rows; ++k) with the same conditional. For i == 1 only the j loop emits the single bottom star.
Newline & width
cout << "\n" after both segments. Line length stays 2 * rows - 1 for a stable silhouette.
Hollow V
O(rows²) character writes, O(1) extra space. Combine with the inverted-V half to form a hollow diamond. Wide rows scroll in the green glyph on phones.
Variation — User Input Version
Accept rows with cin:
#include <iostream>
using namespace std;
int main() {
int rows;
int i, j, k;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = rows; i >= 1; --i) {
for (j = rows; j >= 1; --j) {
if (i == j) cout << "*";
else cout << " ";
}
for (k = 2; k <= rows; ++k) {
if (i == k) cout << "*";
else cout << " ";
}
cout << "\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.Next: Hollow Diamond Pattern
Continue to Program 9 to combine the upper and lower halves into a hollow diamond.
If you start the same descending row loop at rows - 1 (instead of rows), you get the lower half of the hollow diamond without printing the middle row twice.
12 people found this page helpful
