Hollow Diamond Inside Square Star Pattern in C#

Beginner
⏱️ 10 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2n wide, 2n − 1 tall

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:

Output
**********
****  ****
***    ***
**      **
*        *
**      **
***    ***
****  ****
**********
1

Complete C# Program

Fixed rows = 5 version:

C#
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

1

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.

Dimensions
2

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.

Border
3

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.

Symmetry
4

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.

Line break
=

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.

2

Variation — User Input Version

Read rows with Console.ReadLine():

C#
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 < 1 after parsing
  • Rewrite the inner part as one loop over j = 1width with 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.TryParse for safer input

Avoid

  • Mixing up 2 * rows - 1 (line count) with 2 * rows (line width)—both appear in this pattern
  • Copy-pasting i == j / i + j == rows + 1 grid logic from generic tutorials; that pattern uses a different coordinate system than this 10-wide picture for rows = 5
  • Trailing extra space loops after the right star block (they break alignment)

Key Takeaways

1

Width 2n, height 2n - 1 for n = rows.

2

First and last lines: solid bars of *.

3

Other lines: left stars, gap spaces, left stars with i mirrored.

4

Not the same as Program 9’s diagonal-only loop pattern.

5

Time complexity O(n²).

❓ Frequently Asked Questions

Solid rows cap the top and bottom. On inner rows, two equal star runs sit on the left and right; the space between them is the hollow region. The run length and gap follow left and gap from the formulas above, with i mirrored below the waist row.
The width is 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.
Program 9 is a hollow diamond alone with uniform line width 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.
O(n²) for 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.

All C# Star Patterns →
Did you know?

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.

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