Inverted V-Shaped Hollow Star Pattern in C++

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
n rows total

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:

Output
    *    
   * *   
  *   *  
 *     * 
*       *
1

Complete C++ Program

Fixed rows = 5 version:

C++
#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

1

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.

Outer
2

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.

Left
3

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.

Right
4

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.

Width
=

Hollow inverted V

O(rows²) writes, O(1) extra space. Full-width lines scroll horizontally in the green preview on narrow viewports.

2

Variation — User Input Version

Accept rows with cin:

C++
#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 >= 1 after input
  • Parameterize both loops with rows and trace i == j / i == k on 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 = 1 without adjusting logic (duplicate * on row 1)
  • Mixing up j descending vs ascending—the pattern assumes j goes rows → 1
  • Forgetting spaces so columns collapse
  • Assuming cin always succeeds

Key Takeaways

1

Left leg: loop j from rows to 1; star when i == j.

2

Right leg: loop k from 2 to rows; star when i == k.

3

Line width is rows + (rows - 1) = 2 * rows - 1 characters.

4

Time complexity O(n²) for n rows.

5

Often taught as the top half of a hollow diamond.

❓ Frequently Asked Questions

Each inner loop prints at most one * 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.
That order fills the left block of columns so the i == j hit lands on the correct column for each row, forming the left arm of the inverted V.
On row 1 the first loop already outputs the single top star. If k started at 1, i == k would fire again and duplicate it.
O(n²) for 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++.

Program 8 →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful