Inverted V-Shaped Hollow Star Pattern in C

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
if (i == j) / if (i == k)

What You'll Learn

How to build a hollow inverted V: one star on the first row, then the two legs move outward row by row until the base row shows stars at both sides. Two inner loops per row print * or space from i == j and i == k. The example uses rows = 5; each line has 2 * rows - 1 characters (here, 9).

Compare Program 8 for the upright V (wide top, narrow bottom). Together they form the top and bottom halves of a hollow diamond in Program 9.

⭐ Pattern Output

When you run the program with rows = 5, you’ll see (each line is 9 characters wide):

Output
    *    
   * *   
  *   *  
 *     * 
*       *
1

Complete C Program

Fixed rows = 5 version:

c
#include <stdio.h>

int main(void) {
    int i, j, k;
    int rows = 5;

    for (i = 1; i <= rows; ++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

1

Outer Loop (Rows)

for (i = 1; i <= rows; ++i) walks from the narrow top row (i == 1, single star) down to the wide base (i == rows, two outer stars).

Row index
2

Left segment (j)

for (j = rows; j >= 1; --j): printf("*") when i == j, else printf(" "). That draws the left leg of the widening V.

Left leg
3

Right segment (k)

for (k = 2; k <= rows; ++k): printf("*") when i == k, else space. Starting at 2 avoids a second star on row 1 (the apex already came from the j pass).

Right leg
4

New line & width

printf("\n") ends each row. The j loop runs rows times and the k loop rows - 1 times, so every line has 2 * rows - 1 columns (9 when rows = 5).

Fixed width
=

Hollow between the legs

Stars only on the two diagonals (i == j and i == k); everything else is a space. O(rows²) character writes, O(1) extra memory. Full-width rows scroll sideways in the green preview on narrow screens.

2

Variation — User Input Version

Replace the constant with scanf (same logic, rows instead of 5):

c
#include <stdio.h>

int main(void) {
    int rows;
    int i, j, k;

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = 1; i <= rows; ++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

  • 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 scanf 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.

Explore More C Star Patterns!

Hollow diamonds and banners reuse the same idea: decide star vs space with row and column indices.

All Star Patterns →
Did you know?

The same inverted V can be drawn with different loop orders; this version is compact because each row places at most two stars using simple i == j and i == k tests.

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