Inverted V-Shaped Hollow Star Pattern in C#

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):
*
* *
* *
* *
* *Complete C# Program
Fixed rows = 5 version:
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
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(" ").
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.
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.
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.
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).
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.
Variation — User Input Version
Read rows with Console.ReadLine() (same logic, rows instead of 5):
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 >= 1after parsing - Trace
i == jandi == kon paper for smallrows - Add Program 8 below for the V-shaped half of a full hollow diamond
- Print row numbers at star positions instead of
* - Use
int.TryParsefor safer input
Avoid
- Starting the second loop at
k = 1without changing the condition (duplicate*on row 1) - Mixing up
jdescending vs ascending—this pattern assumesjgoesrows → 1 - Omitting spaces so columns collapse
Key Takeaways
Left leg: loop j from rows to 1; star when i == j.
Right leg: loop k from 2 to rows; star when i == k.
Line width is rows + (rows - 1) = 2 * rows - 1 characters.
Time complexity O(n²) for n rows.
Often taught as the top half of a hollow diamond.
❓ Frequently Asked Questions
* 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.i == j hit lands on the correct column for each row, forming the left leg of the inverted V.k started at 1, i == k would fire again and duplicate it.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.
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.
12 people found this page helpful
