Inverted V-Shaped Hollow Star Pattern in C

Beginner
⏱️ 10 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
if (i == j)

What You’ll Learn

A hollow inverted V starts with one apex star and widens: each row prints at most two stars via if (i == j) and if (i == k), with spaces everywhere else. This tutorial covers both legs, why k starts at 2, a live preview, algorithm steps, worked C examples, edge cases, and complexity.

Shape Rule

Apex on top

One star on row 1; two stars farther apart on each row below.

Left Leg

j = rows..1

Print * only when i == j; otherwise a space.

Right Leg

k = 2..rows

Mirror slant; start at 2 so the apex is not duplicated.

Line Width

2n - 1

Left block rows + right block rows - 1 characters.

Live Preview

1–14 rows

Pick a height and draw the hollow inverted V instantly.

O(n²)

Diamond half

O(n²) time; upper half of Program 9’s hollow diamond.

Introduction

A hollow inverted V draws only the outline: one apex on top, then two stars per row that drift farther apart toward the base.

Unlike solid pyramids, every cell chooses * or space from an equality test. Flip the outer loop in Program 8 for an upright V, or stack both halves for the hollow diamond.

Why it matters?

Conditional printing (if star else space) is the core skill behind hollow shapes. Once i == j / i == k clicks, diamonds and banners become composition problems.

Key Highlights

Two Legs

Left j block + right k block.

Equality Tests

Star only when row equals column index.

k Starts at 2

Prevents a double apex on row 1.

Diamond Upper Half

Building block for Program 9.

In short: for each row i, scan left columns j = rows..1 and right columns k = 2..rows; print * when the column index equals i, else a space.

📝 Problem & Approach

Given a positive integer rows, print a hollow inverted V of * characters with rows lines and width 2 * rows - 1.

c
// First 5 rows (spaces shown as ·)
// ····*····
// ···*·*···
// ··*···*··
// ·*·····*·
// *·······*

Inputs & Outputs

ItemTypeDescription
rowsintHeight of the inverted V (typically ≥ 1). Line width is 2 * rows - 1.
Printed outputtextHollow outline: stars on diagonals only; interior spaces.

Minimal workflow

Pseudocode
for i from 1 to rows:
    for j from rows down to 1:
        print "*" if i == j else " "
    for k from 2 to rows:
        print "*" if i == k else " "
    print newline

Approach comparison

ApproachIdeaBest for
Two loops + if/elseLeft and right segments separatelyLearning and interviews
Ternary shortcutSame loops; i == j ? "*" : " "Shorter demos after conditions click

⚡ Quick Reference

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Left leg columnsfor (j = rows; j >= 1; j--) + if (i == j)
Right leg columnsfor (k = 2; k <= rows; k++) + if (i == k)
Line width2 * rows - 1
Flip to upright Vfor (i = rows; i >= 1; i--) (see Program 8)
Ternary formprintf("%s", (i == j) ? "*" : " ");

📋 Inverted V vs V vs Hollow Diamond

Same inner equality tests — outer-loop direction and stacking define the family.

This page
i = 1..rows

Inverted V — apex on top

Program 8
i = rows..1

Upright V — vertex at bottom

Program 9
+ lower half

Hollow diamond — this page on top

Program 6
solid fill

Inverted pyramid — stars, not outline

Context

When This Pattern Shows Up

Reach for a hollow inverted V when teaching conditional printing after solid pyramids.

  1. After solid pyramids

    Natural step once Programs 5–6 are solid.

  2. Conditional printing labs

    if star else space is a classic nested-loop interview warm-up.

  3. Hollow diamond precursor

    Upper half of Program 9’s hollow diamond.

  4. Diagonal index practice

    Matching row and column indices builds 2D thinking.

  5. Not a UI layout tool

    Terminal teaching pattern — not how you build app screens.

Key benefit: one outline that locks in star-vs-space decisions — the gateway to hollow diamonds.

🔮 Live Preview

Choose a height between 1 and 14 and draw the hollow inverted V in the browser.

Try 4, 5, or 7. Each line width will be 2 * rows - 1.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C programs — classic if/else legs, console input, and a ternary shortcut. Click View Output to reveal sample console results.

📚 Getting Started

Print a five-row hollow inverted V with nested loops and if/else.

Example 1 — Fixed rows = 5

Left loop j = rows..1; right loop k = 2..rows; star when indices match.

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

When i = 1, only j == 1 prints a star — the apex. When i = 5, stars land at the outer columns of both blocks. Each line is 9 characters wide (2 * 5 - 1).

📈 Practical Variant

Let the user choose the height at runtime.

Example 2 — User Input Version

Read rows with scanf("%d", &rows) (check the return value in real apps).

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;
}

How It Works

Same left/right leg core as Example 1; only the source of rows changes. Non-numeric input leaves rows unset if you ignore scanf’s return value — always check it in safer labs.

⚡ Shortcut Style

Same outline with ternary operators instead of multi-line if/else.

Example 3 — Ternary ? : Form

Keep both loops; compress the star-vs-space choice into one expression each.

c
#include <stdio.h>

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

    for (i = 1; i <= rows; ++i) {
        for (j = rows; j >= 1; --j) {
            printf("%s", (i == j) ? "*" : " ");
        }
        for (k = 2; k <= rows; ++k) {
            printf("%s", (i == k) ? "*" : " ");
        }
        printf("\n");
    }

    return 0;
}

How It Works

Same bounds and conditions as Example 1; only the print statement is shorter. Keep the if/else version for exams that want the branch structure spelled out.

🧠 How the Algorithm Prints Rows

1

Set up

Set rows. Use i for the row, j for the left block, k for the right block.

Setup
2

Outer loop

for (i = 1; i <= rows; i++) — apex when i == 1, widest gap when i == rows.

Row
3

Left leg

for (j = rows; j >= 1; j--) with if (i == j) draws the descending left diagonal.

Left
4

Right leg then newline

for (k = 2; k <= rows; k++) with if (i == k), then printf("\n"). Start at 2 to skip a duplicate apex.

Right
=

Hollow inverted V

Only diagonal positions get *. O(n²) time, O(1) extra space. Width 2n - 1.

🔎 Worked Walkthrough — rows = 4

Trace where each star lands for every outer-loop value of i (line width = 7).

iLeft star (j)Right star (k)Stars on rowPrinted row
1j == 1none (k starts at 2)1   *   
2j == 2k == 22  * *  
3j == 3k == 32 *   * 
4j == 4k == 42*     *

Row 1 is the only single-star line — that is why the right loop must not start at k = 1.

Use Cases

Where this hollow inverted V (and diagonal conditions) shows up beyond the homework prompt.

1. Teaching if/else Printing

Every cell is an explicit star-or-space decision.

Example: dry-run i == j on paper for rows = 4.

2. Hollow Diamond Upper Half

Reuse this body, then add Program 8 from rows - 1.

Example: Program 9.

3. Flip to Upright V

Countdown outer loop keeps the same inners.

Example: Program 8.

4. Compare With Solid Pyramid

Program 6 fills every star; this page keeps only the outline.

Example: side-by-side for rows = 5.

5. Numbered Diagonals

Print i instead of * at match positions.

Example: visualize which row owns each star.

6. Input Validation Labs

Pair with a scanf return-value check and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: say “star when i equals the column index” before coding — that is the whole outline rule.

Advantages

Why the hollow inverted V is a favorite mid-series pattern.

  1. 1. Clear Conditional Logic

    Equality tests make outline vs fill an explicit choice.

  2. 2. Instant Visual Feedback

    Wrong k start or loop order looks obviously broken.

  3. 3. Unlocks Hollow Diamonds

    Same legs power Program 8 and Program 9.

  4. 4. Fixed Line Width

    Every row is 2n-1 chars — easy to verify alignment.

Pro Tip: master the if/else version first; treat ternaries as a polish shortcut afterward.

Usage Tips

Small habits that keep hollow-outline code clean.

  1. 1. Keep k Starting at 2

    Starting at 1 duplicates the apex on row 1.

  2. 2. Descend j From rows

    This pattern assumes j walks rows → 1 for the left leg.

  3. 3. Check scanf’s return value

    Avoid crashes when the user types letters instead of a number.

  4. 4. Always Print Spaces

    Skipping the else branch collapses columns and ruins the V.

  5. 5. Dry-Run One Small n

    Trace rows = 4 star positions before coding larger demos.

Pro Tip: if row 1 shows two stars side by side, you almost certainly started k at 1.

Common Pitfalls

Mistakes that commonly break hollow inverted V patterns.

  1. 1. Starting k at 1

    Duplicates the apex: row 1 prints two stars.

    → Use for (k = 2; k <= rows; k++).

  2. 2. Ascending j Instead of Descending

    Wrong column order shifts or mirrors the left leg.

    → Keep for (j = rows; j >= 1; j--).

  3. 3. Omitting the else Space Branch

    Columns collapse; the V becomes a left-aligned smear.

    → Always print " " when the equality fails.

  4. 4. Mixing Tabs With Spaces

    Alignment looks fine in one editor and broken in another.

    → Always print the space character " ".

  5. 5. Blind scanf

    Failed scanf leaves rows uninitialized.

    → Check scanf’s return value and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single apex

Left loop prints one *; right loop (k = 2..1) never runs.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Wide lines

Width 2n-1 — fine for labs; may wrap on tiny terminals.

Bad input

Non-numeric scanf

Failed scanf leaves rows unset — check its return value.

Last row

i == rows

Stars at both outer columns — widest gap of the inverted V.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Flip to Program 8

  • Change outer loop to i = rows..1
  • Confirm upright V with bottom vertex

2. Break then fix the apex

  • Temporarily start k at 1
  • See the double star, then restore k = 2

3. Print row numbers

  • Write i instead of * at match positions
  • Check that diagonals show increasing values

4. Build a hollow diamond

  • Add lower half from rows - 1 down to 1
  • Match Program 9

Notes

  • Fixed width. Every row prints exactly 2 * rows - 1 characters before the newline.
  • Star count is 1 on row 1 and 2 on every later row — total stars = 2 * rows - 1.
  • Validate rows > 0 for interactive programs; rows = 1 prints a single centered-looking apex in a 1-char line.
  • Next: Program 8 flips this outline into an upright V with the same inner loops.

Quick Takeaway: print * when i equals the column index in each leg — left j = rows..1, right k = 2..rows.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Two inner loops + if/else (Examples 1–2)O(rows²)O(1)
Ternary shortcut (Example 3)O(rows²)O(1)

Each of n rows runs Θ(n) iterations across left + right blocks. Only 2n - 1 stars are printed, but every cell is visited.

Wrap Up

🎉 Conclusion

The hollow inverted V is conditional printing on two legs: star when i matches the column index, space otherwise. Keep k starting at 2, and the apex stays a single star — flip the outer loop next for an upright V or stack halves for a diamond.

Practice the three examples above, then continue to the V-shaped hollow pattern.

Left descends, right starts at 2, width = 2n−1 — keep the equality tests, and validate row counts when reading input.

💡 Best Practices

✅ Do

  • Explain left and right legs before coding
  • Start the right loop at k = 2
  • Print a space whenever the equality fails
  • State line width as 2 * rows - 1
  • Check scanf’s return value for interactive demos

❌ Don’t

  • Start k at 1 without adjusting the condition
  • Skip spaces so columns collapse
  • Reverse j without checking the left-leg shape
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 apex edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about the hollow inverted V

Print the outline the beginner-friendly way.

5
Core concepts
L 02

Left

j = rows..1

Leg
R 03

Right

k = 2..rows

Leg
W 04

Width

2n - 1

Layout
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

The outer loop runs i from 1 to rows, so the first row is narrow and each later row places its two stars farther apart. For each row i, the first inner loop runs j from rows down to 1 and prints a star only if i equals j. The second inner loop runs k from 2 to rows and prints a star only if i equals k. Every other position prints a space.
Printing columns from high j to low j orders the left segment so the star for row i lands in column j when i equals j. As i increases, the match moves leftward in that segment, forming the left leg of the inverted V.
On row 1, the first loop already prints the apex at j equals 1. If the second loop started at k equals 1, the condition i equals k would print a second star on the same row. Starting at k equals 2 avoids duplicating the top star.
Each line has width rows + (rows - 1) = 2 * rows - 1 characters: left block length rows, right block length rows - 1.
With n rows, each row runs Theta(n) iterations across the two inner loops, so total time is O(n²).
Program 8 uses the same inner loops but counts the outer loop from rows down to 1, so the wide row prints first and the legs meet at a bottom vertex.
This page is the upper half of Program 9. Stack Program 8 (from rows-1 down to 1) underneath to complete the diamond.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input. Unchecked scanf leaves rows uninitialized on failure.

Did you Know? 🔊

This hollow inverted V is the upper half of the hollow diamond. Starting the right loop at k = 2 is deliberate: on row 1 the left loop already prints the apex, so k = 1 would duplicate that star.

Continue to V-Shaped Hollow

Keep the same inner loops and count the outer loop down for an upright V with a bottom vertex.

Program 8 tutorial →

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