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 (narrow top row, legs widen toward the base): two inner loops per row, each printing either * or a space from if (i == j) and if (i == k). The example uses rows = 5; each line has 2 * rows - 1 characters (here, 9).

See Program 8 for the upright V (wide row first, vertex at the bottom). This page is the usual top half of a hollow diamond.

⭐ 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#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, k;
            int rows = 5;

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

🧠 How It Works

1

Setup

rows sets the height. i is the row index (1 to rows). j scans the left block; k scans the right block. Each cell is either Console.Write("*") or Console.Write(" ").

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; i++) grows the gap between the two legs: row 1 is the apex; row rows has stars at both outer columns.

Row index
3

Left leg: j from rows down to 1

for (j = rows; j >= 1; j--) with if (i == j) Console.Write("*"); else Console.Write(" "); draws the descending diagonal: one * per row on the left side of the apex block.

Left leg
4

Right leg: k from 2 to rows

for (k = 2; k <= rows; k++) with the same if / else prints the mirror leg. Starting at k = 2 avoids duplicating the top star; for i > 1 you get two stars per row separated by spaces.

Right leg
5

Finish the line

Console.WriteLine() ends the row. Each line has width rows + (rows - 1) = 2 * rows - 1 characters (left block length rows, right block rows - 1).

Line break
=

Hollow inverted V

Only diagonal positions get *; the interior is spaces. O(n²) output for n = rows, O(1) extra space. Full width 2n - 1 scrolls inside the green preview on phones.

2

Variation — User Input Version

Read rows with Console.ReadLine() (same logic, rows instead of 5):

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows;
            int i, j, k;

            Console.Write("Enter the number of rows: ");
            rows = Convert.ToInt32(Console.ReadLine());

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

💡 Tips for Enhancement

Try These

  • Validate rows >= 1 after parsing
  • Trace i == j and i == k on paper for small rows
  • Add Program 8 below for the V-shaped half of a full hollow diamond
  • Print row numbers at star positions instead of *
  • Use int.TryParse for safer input

Avoid

  • Starting the second loop at k = 1 without changing the condition (duplicate * on row 1)
  • Mixing up j descending vs ascending—this pattern assumes j goes rows → 1
  • Omitting spaces so columns collapse

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 leg 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: choose star vs space from row and column indices.

All C# Star Patterns →
Did you know?

The same inverted-V outline can be drawn with different loop orders; this version stays short because each segment prints at most one star per row via a simple equality test.

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