C# Hollow Star Pattern (Inverted V)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An inverted V-shaped hollow pattern prints only the outline of an upside-down V: a single apex on row 1, then two stars that drift farther apart on each later row.

Remember
Rule: star when i == j (left) or i == k (right); else space

    *    
   * *   
  *   *  
 *     * 
*       *     ← 5 rows (width 9)

Unlike the filled inverted pyramid in Program 6, most cells are spaces. This outline is also the upper half of the hollow diamond — flip the outer loop in Program 8 to get the matching upright V.

How to Solve It

Two ways to emit the same outline — start with if/else legs, then optionally shorten with a ternary.

MethodIdeaBest for
If/else legsLeft j and right k loops; star when indices matchLearning, interviews, exams
Ternary ? :Same bounds; one-line star-vs-space choiceShorter demos once conditions click

Pseudocode

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

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Left legfor (j = rows; j >= 1; j--) + if (i == j)
Right legfor (k = 2; k <= rows; k++) + if (i == k)
Line width2 * rows - 1
End the rowConsole.WriteLine();
Ternary shortcutConsole.Write(i == j ? "*" : " ");
Flip laterfor (i = rows; i >= 1; i--) → Program 8

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach * and each space
Console.WriteLineEnds the current lineAfter both inner loops

Live Preview

Change the height and the hollow inverted V updates instantly — including width and star count.

Whole numbers from 1 to 14. Each line is 2 * rows - 1 characters wide.

Live result 5 rows · 9 stars
    *    
   * *   
  *   *  
 *     * 
*       *

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)StarsPrinted 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. Total stars: 1 + 2 + 2 + 2 = 7 = 2×4 - 1.

C# Programs

Three complete programs: classic if/else, console input, and a ternary shortcut. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — left loop j = rows..1, right loop k = 2..rows, star when indices match.

C#
using System;

class Program
{
    static void Main()
    {
        int rows = 5;

        for (int i = 1; i <= rows; i++)
        {
            for (int j = rows; j >= 1; j--)
            {
                if (i == j)
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
            for (int k = 2; k <= rows; k++)
            {
                if (i == k)
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
            Console.WriteLine();
        }
    }
}

How It Works

1. Set height. rows = 5 means five outline lines (width 9).

2. Outer loop picks the row. i runs from 1 (apex) to rows (widest gap).

3. Left leg. j counts from rows down to 1; print * only when i == j.

4. Right leg, then break. k runs from 2 to rows with the same match rule, then WriteLine.

When i = 1 only the left loop prints a star; when i = 5 stars land at both outer columns.

Example 2 — User Input Version

Read the height at runtime. Prefer int.TryParse in real apps (shown in the tip below).

C#
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter the number of rows: ");
        int rows = Convert.ToInt32(Console.ReadLine());

        for (int i = 1; i <= rows; i++)
        {
            for (int j = rows; j >= 1; j--)
            {
                if (i == j)
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
            for (int k = 2; k <= rows; k++)
            {
                if (i == k)
                    Console.Write("*");
                else
                    Console.Write(" ");
            }
            Console.WriteLine();
        }
    }
}

How It Works

1. Prompt and read. Ask for a row count, then convert the line to an int.

2. Same left/right core. Only the source of rows changes — the leg logic matches Example 1.

3. Safer input tip. Convert.ToInt32 throws on letters or empty input. Prefer:

Safer input
if (!int.TryParse(Console.ReadLine(), out int rows) || rows < 1)
{
    Console.WriteLine("Enter a positive whole number.");
    return;
}

Example 3 — Ternary ? : Form

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

C#
using System;

class Program
{
    static void Main()
    {
        int rows = 5;

        for (int i = 1; i <= rows; i++)
        {
            for (int j = rows; j >= 1; j--)
                Console.Write(i == j ? "*" : " ");
            for (int k = 2; k <= rows; k++)
                Console.Write(i == k ? "*" : " ");
            Console.WriteLine();
        }
    }
}

How It Works

1. Same outer loop. Still walk i from 1 to rows.

2. Same bounds. Left j still counts down; right k still starts at 2.

3. Shorter print. i == j ? "*" : " " replaces the multi-line if/else — same decision, less code.

Learn the if/else version first (Examples 1–2) so you can explain the branch in an interview; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

k = 1

Duplicate apex

Starting the right loop at k = 1 prints two stars on row 1. Keep k = 2.

j ascending

Mirrored left leg

The left loop must count j from rows down to 1. Ascending j flips the left diagonal.

WriteLine inside

Broken outline

If WriteLine is inside either inner loop, each cell lands on its own line. Use Write for cells; WriteLine only after both loops.

rows = 1

Single apex

Output is just * — right loop never runs. A good sanity check.

rows ≤ 0

Empty output

Outer loop never runs. Validate and re-prompt for interactive programs.

Bad input

Use TryParse

Convert.ToInt32 throws on letters — prefer int.TryParse.

Time and Space Complexity

ProgramTimeExtra space
If/else legs (Examples 1–2)O(rows²)O(1)
Ternary form (Example 3)O(rows²)O(1)

About n rows × 2n - 1 characters printed per row — still quadratic in n. Total stars = 2n - 1 (one apex + two per later row).

Key Takeaways

  • Rule: print * only when i == j (left) or i == k (right).
  • Two legs: left j counts down; right k starts at 2.
  • Break the row: call WriteLine only after both inner loops.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row i, print a star only when the left or right index matches i — start the right loop at 2.

Frequently Asked Questions

The outer loop runs i from 1 to rows. For each row, the left loop runs j from rows down to 1 and prints a star only when i equals j. The right loop runs k from 2 to rows and prints a star only when i equals k. Every other cell is a space.
Printing columns from high j to low j places the star for row i when i equals j. As i grows, that match moves leftward in the left block, forming the descending left leg.
On row 1 the left loop already prints the apex at j equals 1. Starting k at 1 would print a second star on that row. Starting at 2 avoids duplicating the tip.
Console.Write stays on the same line. Console.WriteLine ends the current line. Stars and spaces use Write; the row break uses WriteLine after both inner loops.
Each line has width 2 * rows - 1: left block length rows, right block length rows - 1.
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.
O(n²) for n rows. Each row runs Theta(n) iterations across the two inner loops.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.

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.

Next: V-Shaped Hollow

Reverse the outer loop and print the matching upright V outline.

Program 8 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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