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 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:

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 = 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

1

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.

Outer
2

Left leg (j)

for (j = rows; j >= 1; --j) with if (i == j) cout << "*"; else cout << " "; draws the left diagonal.

Left
3

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.

Right
4

Newline & width

cout << "\n" after both segments. Line length stays 2 * rows - 1 for a stable silhouette.

Width
=

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.

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 = 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..rows to recover Program 7 (inverted V)
  • Validate rows >= 1 after input
  • Stack Program 7 (inverted V) then Program 8 (upright V), or see Program 9 for the full hollow diamond
  • Print row index i where stars appear

Avoid

  • Claiming two stars on the bottom row from both loops—only j hits i == 1
  • Incrementing i in the outer loop (that produces Program 7’s inverted V, not this upright V)
  • Omitting spaces so columns no longer align

Key Takeaways

1

Same inner logic as Program 7 (inverted V); only outer loop direction changes to flip the shape upright.

2

Top row (i == rows): stars at j == rows and k == rows.

3

Bottom row (i == 1): one star from j; k loop is all spaces.

4

Line width remains 2 * rows - 1 characters.

5

Time complexity O(n²) for n rows.

❓ Frequently Asked Questions

Large i values run first, so the row with two outer stars (i == rows) is printed before smaller i values pull the legs inward.
Program 7 uses 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.
For 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.
O(n²) for 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.

Program 9 →
Did you know?

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.

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.

12 people found this page helpful