Inverted V-Shaped Hollow Star Pattern in C++

What You'll Learn
This program prints a hollow inverted V (one star on the first row, then two stars per row farther apart) using only the diagonal checks i == j and i == k.
See Program 8 for the upright V (wide row first, vertex at the bottom). Each line has width 2 * rows - 1 (9 characters when rows = 5).
⭐ 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 = 1; i <= rows; ++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: narrow top → wide base
for (i = 1; i <= rows; ++i) increases the row index so the two legs move apart. cout << "*" or cout << " " fills each cell.
Left leg (j)
for (j = rows; j >= 1; --j): if i == j then cout << "*", else space. That traces the downward diagonal on the left half.
Right leg (k)
for (k = 2; k <= rows; ++k) mirrors the test: star when i == k. Starting at 2 avoids duplicating the apex star on row 1.
Newline & width
cout << "\n" ends the row. The j loop emits rows characters and the k loop rows - 1, so width is always 2 * rows - 1.
Hollow inverted V
O(rows²) writes, O(1) extra space. Full-width lines scroll horizontally in the green preview on narrow viewports.
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 = 1; i <= rows; ++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
- 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
cinalways 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.Next: V-Shaped Hollow Pattern
Continue to Program 8 to print the upright V-shaped hollow pattern in C++.
If you print this upper half and then print Program 8 starting from rows - 1, you get the full hollow diamond (Program 9) without duplicating the middle row.
13 people found this page helpful
