C# Number-Star Diamond Pattern

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

What Is This Pattern?

A number-star diamond grows from 1 to n, then shrinks back to 1. Each row repeats the row number i times, separated by *.

Remember
Rule: 2*i-1 chars — odd j → i, even j → *

1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1   ← peak n = 5

In C# use two outer loops: up 1..n, then down n-1..1. The shared inner idea is j % 2 to pick a digit or a star.

How to Solve It

One row-builder idea reused for the top and bottom halves.

MethodIdeaBest for
If/else + modulusEven j → *; odd j → iLearning, interviews, exams
Ternary formSame logic in one expression per cellShorter demos

Pseudocode

Pseudocode
for i from 1 to n:
    for j from 1 to 2*i-1:
        if j is even: print * else print i
    print newline
for i from n-1 down to 1:
    for j from 1 to 2*i-1:
        if j is even: print * else print i
    print newline

Cheat sheet

GoalPattern
Top halffor (i = 1; i <= n; i++)
Bottom halffor (i = n - 1; i >= 1; i--)
Row lengthfor (j = 1; j < i * 2; j++)
Digit or starif (j % 2 == 0) Console.Write("*"); else Console.Write(i);
Ternary formConsole.Write((j % 2 == 0) ? "*" : i.ToString());
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit or *
Console.WriteLineEnds the current lineAfter the inner loop

Live Preview

Change the peak height and the diamond updates instantly — including total row count.

Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.

Live result peak 5 · 9 rows
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1

Worked Walkthrough — n = 4

Trace the top climb and the mirrored bottom. Each row length is 2*i-1.

HalfiCharsPrinted row
Top111
Top232*2
Top353*3*3
Peak474*4*4*4
Bottom353*3*3
Bottom232*2
Bottom111

Total lines = 2n - 1. The peak row is printed once (only in the top loop).

C# Programs

Three complete programs: fixed peak, user input with a ternary, and a compact 3-peak demo. Use View Output for sample results.

Example 1 — Fixed n = 5

Top loop climbs to 5; bottom loop mirrors from 4 down to 1. Even j prints *.

C#
using System;

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

            for (i = 1; i <= 5; i++)
            {
                for (j = 1; j < i * 2; j++)
                {
                    if (j % 2 == 0)
                        Console.Write("*");
                    else
                        Console.Write(i);
                }

                Console.WriteLine();
            }

            for (i = 4; i >= 1; i--)
            {
                for (j = 1; j < i * 2; j++)
                {
                    if (j % 2 == 0)
                        Console.Write("*");
                    else
                        Console.Write(i);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Top half. i grows from 1 to 5. Each row prints 2*i-1 characters.

2. Alternation. Odd j prints i; even j prints * — e.g. 3*3*3.

3. Bottom half. i shrinks from 4 to 1 with the same inner logic, completing the diamond.

Example 2 — User Input

Read the peak height and use a ternary for compact digit-or-star logic.

C#
using System;

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

            Console.Write("Enter n: ");
            if (!int.TryParse(Console.ReadLine(), out n) || n < 1)
            {
                Console.WriteLine("Please enter a positive whole number.");
                return;
            }

            for (i = 1; i <= n; i++)
            {
                for (j = 1; j < i * 2; j++)
                    Console.Write((j % 2 == 0) ? "*" : i.ToString());

                Console.WriteLine();
            }

            for (i = n - 1; i >= 1; i--)
            {
                for (j = 1; j < i * 2; j++)
                    Console.Write((j % 2 == 0) ? "*" : i.ToString());

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Validate n. TryParse rejects non-numeric input; require n >= 1.

2. Same diamond. Ternaries pick digit vs star; peak 3 produces five lines total.

Example 3 — Compact n = 3

A smaller if/else demo — same two-half idea, easier to trace by hand.

C#
using System;

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

            for (i = 1; i <= n; i++)
            {
                for (j = 1; j < i * 2; j++)
                {
                    if (j % 2 == 0) Console.Write("*");
                    else Console.Write(i);
                }

                Console.WriteLine();
            }

            for (i = n - 1; i >= 1; i--)
            {
                for (j = 1; j < i * 2; j++)
                {
                    if (j % 2 == 0) Console.Write("*");
                    else Console.Write(i);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same rules. Odd positions print i; even positions print *.

2. Mirror down. After the peak, the second loop reprints rows 2 and 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

double peak

Bottom loop starts at n

That reprints the peak. Start the bottom at n - 1.

wrong length

j <= i instead of j < i * 2

You need 2*i-1 characters (digits and stars). Keep for (j = 1; j < i * 2; j++).

flip parity

Swap even/odd checks

Even j must print *. Swapping gives *2* instead of 2*2.

n = 1

Single 1

Only the top loop prints once; the bottom loop never runs.

n ≤ 0

Empty output

Neither loop runs. Validate and prompt again for clearer UX.

Bad input

Convert.ToInt32 throws

Prefer int.TryParse so non-numeric input does not crash the program.

Time and Space Complexity

ProgramTimeExtra space
If/else loops (Examples 1, 3)O(n²)O(1)
Ternary form (Example 2)O(n²)O(1)

Across both halves the printed characters sum to about 2n², so work is quadratic in n.

Key Takeaways

  • Rule: odd j prints i; even j prints *.
  • Two halves: climb 1..n, then descend n-1..1 so the peak prints once.
  • Write vs WriteLine: digits and stars stay on the line; WriteLine advances after each row.
  • Next step: Program 32 prints an increasing number triangle starting from 11.

One line: for each row value i, print 2*i-1 characters alternating i and *, then mirror the climb back down.

Frequently Asked Questions

The inner loop runs while j < i*2, which prints 1, 3, 5, 7, 9 characters for i = 1..5.
It checks j % 2. Even j prints '*', odd j prints the current row number i.
The first loop builds the top half (i = 1..n). The second mirrors back down (i = n-1..1) to complete the diamond.
Program 30 is a right-aligned descending triangle. Program 31 alternates digits and stars in a symmetric diamond shape.
Replace 5 with n in both outer loops — see Example 2.
O(n²) for n rows because total printed characters grow quadratically across both halves.
Prefer int.TryParse(Console.ReadLine(), out n) so bad input does not throw FormatException.
Only one row prints — a single 1.
Yes — Console.Write((j % 2 == 0) ? "*" : i.ToString()) compacts the if/else logic.

Did you know?

This pattern prints a top half (1..n) and a bottom half (n-1..1). Each row prints 2*i-1 characters, alternating the row number and * using j % 2.

Next: Increasing Triangle from 11

Print a growing triangle of two-digit numbers starting at 11.

Program 32 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.

6 people found this page helpful