Hollow Diamond Inside Square Star Pattern in C#

What You'll Learn
You print a frame with a solid row of * on the first line and again on the last line, and mirror the rows in between so the middle row is narrowest (only the left and right border stars with a wide hollow gap). The width is 2 * rows; the number of lines is 2 * rows - 1.
This is not the same loop structure as Program 9: there the diamond stands alone with constant width 2 * rows - 1 per line. Here the layout matches the classic “diamond inside a box” picture when rows = 5.
⭐ Pattern Output
When you run the program with rows = 5:
**********
**** ****
*** ***
** **
* *
** **
*** ***
**** ****
**********Complete C# Program
Fixed rows = 5 version:
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int line, j;
int height = 2 * rows - 1;
int width = 2 * rows;
for (line = 1; line <= height; line++)
{
if (line == 1 || line == height)
{
for (j = 1; j <= width; j++)
{
Console.Write("*");
}
}
else
{
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
for (j = 1; j <= left; j++)
{
Console.Write("*");
}
for (j = 1; j <= gap; j++)
{
Console.Write(" ");
}
for (j = 1; j <= left; j++)
{
Console.Write("*");
}
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Grid size
height = 2 * rows - 1 lines; width = 2 * rows characters per line (even width for the closed frame). For rows = 4 that is 7 lines × 8 columns, matching the sample output.
Top and bottom bars
When line == 1 or line == height, for (j = 1; j <= width; j++) Console.Write("*"); draws a solid horizontal edge with no interior gap.
Inner rows: left, gap, right
Else branch: map line to i with i = (line <= rows) ? line : (2 * rows - line). Set left = rows - i + 1 and gap = 2 * (i - 1). Three loops: left times Console.Write("*"), gap times Console.Write(" "), then left stars again. The hollow region grows until i == rows, then shrinks symmetrically.
Finish each line
After either the full bar or the three inner segments, Console.WriteLine() advances to the next row. Every line is exactly width = 2 * rows characters wide before the newline.
Hollow center
O(n²) output for n = rows (each of 2n-1 lines writes up to 2n cells), O(1) extra space. Top and bottom bars are the widest; on small screens the green preview uses horizontal scroll so all 2n columns stay readable.
Variation — User Input Version
Read rows with Console.ReadLine():
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int line, j;
int height, width;
Console.Write("Enter the number of rows: ");
rows = Convert.ToInt32(Console.ReadLine());
height = 2 * rows - 1;
width = 2 * rows;
for (line = 1; line <= height; line++)
{
if (line == 1 || line == height)
{
for (j = 1; j <= width; j++)
{
Console.Write("*");
}
}
else
{
int i = (line <= rows) ? line : (2 * rows - line);
int left = rows - i + 1;
int gap = 2 * (i - 1);
for (j = 1; j <= left; j++)
{
Console.Write("*");
}
for (j = 1; j <= gap; j++)
{
Console.Write(" ");
}
for (j = 1; j <= left; j++)
{
Console.Write("*");
}
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Reject
rows < 1after parsing - Rewrite the inner part as one loop over
j = 1…widthwith boolean tests (border OR left block OR right block) - Compare side-by-side with Program 9 output for the same
rows - Swap
*for digits on the border only - Use
int.TryParsefor safer input
Avoid
- Mixing up
2 * rows - 1(line count) with2 * rows(line width)—both appear in this pattern - Copy-pasting
i == j/i + j == rows + 1grid logic from generic tutorials; that pattern uses a different coordinate system than this 10-wide picture forrows = 5 - Trailing extra space loops after the right star block (they break alignment)
Key Takeaways
Width 2n, height 2n - 1 for n = rows.
First and last lines: solid bars of *.
Other lines: left stars, gap spaces, left stars with i mirrored.
Not the same as Program 9’s diagonal-only loop pattern.
Time complexity O(n²).
❓ Frequently Asked Questions
left and gap from the formulas above, with i mirrored below the waist row.2 * rows, so 10 when rows = 5. The height is 2 * rows - 1 (9 lines), so the outer shape uses a wide horizontal bar and matching side columns on the inner rows.2 * rows - 1. This program adds full-width top and bottom rows of length 2 * rows and builds each inner line from left block, gap, and right block.n = rows: Θ(n) lines, each printing Θ(n) characters.You’ve Reached the Last Numbered Star Pattern
Review Programs 9 and 10, then browse the hub for more C# topics.
Many older tutorials show a different listing for this picture; the version here is checked so every inner line satisfies 2 * left + gap == 2 * rows.
12 people found this page helpful
