Number Diamond Pattern in C#

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops + Centering

What You’ll Learn

How to print a centered number diamond in C# by building a pyramid (top half) and then an inverted pyramid (bottom half).

Each row prints an odd count of digits: 1, 3, 5, 7, 9, and so on.

⭐ Pattern Output

For 5 levels, the pattern looks like this:

Output
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1
1

Complete C# Program

Print the top pyramid from i = 1..5, then print the bottom pyramid from i = 4..1.

C#
using System;

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

            for (i = 1; i <= 5; i++)
            {
                for (j = i; j < 5; j++)
                    Console.Write(" ");

                for (k = 1; k < i * 2; k++)
                    Console.Write(k);

                Console.WriteLine();
            }

            for (i = 4; i >= 1; i--)
            {
                for (j = 5; j > i; j--)
                    Console.Write(" ");

                for (k = 1; k < i * 2; k++)
                    Console.Write(k);

                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Top half builds the pyramid

The first outer loop prints rows from 1 to 5.

Top
2

Spaces center the row

The space loop prints indentation so the row is centered.

Centering
3

Odd-length number sequence

The number loop prints 1..(2*i-1) using k < i*2.

Numbers
4

Bottom half mirrors the pyramid

The second outer loop runs from 4 down to 1 to print the inverted half.

Bottom
2

Variation — User Input Levels

Let the user choose the number of levels (height of the top half).

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int levels;
            Console.Write("Enter levels: ");
            if (!int.TryParse(Console.ReadLine(), out levels) || levels <= 0)
            {
                Console.WriteLine("Please enter a positive integer.");
                return;
            }

            for (int i = 1; i <= levels; i++)
            {
                for (int j = i; j < levels; j++)
                    Console.Write(" ");

                for (int k = 1; k < i * 2; k++)
                    Console.Write(k);

                Console.WriteLine();
            }

            for (int i = levels - 1; i >= 1; i--)
            {
                for (int j = levels; j > i; j--)
                    Console.Write(" ");

                for (int k = 1; k < i * 2; k++)
                    Console.Write(k);

                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Print the sequence in reverse (descending) to create a different diamond style
  • Use Console.Write(k + \" \") to add spacing between digits
  • Print stars instead of numbers to create a star diamond

Avoid

  • Putting the newline inside the inner loops
  • Using negative levels without validation

Key Takeaways

1

Top half increases from 1 to levels.

2

Bottom half decreases from levels-1 to 1.

3

Each row prints \(2i-1\) digits.

4

Indentation spaces keep the diamond centered.

Explore More C# Number Patterns!

Diamond patterns are a great way to practice centering logic with spaces and odd-length sequences.

All Number Patterns →
Did you know?

Printing a diamond is usually just two triangles: an increasing triangle and a decreasing triangle joined together.

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