Write A C# Program To Print A Diamond By Using Nested Loop

Do you want to print a diamond by using a nested loop in C#? In this tutorial, I will explain how to write a C# program to print a diamond by using a nested loop.

To print a diamond shape in C# using nested loops, start by obtaining the diamond’s size from the user. Then, use two sets of nested loops: the first set to print the upper triangle and the second set for the lower triangle. Each loop controls the printing of spaces and stars (*) to form the diamond shape.

C# Program To Print A Diamond By Using Nested Loop

A diamond shape is essentially a combination of two triangles – an upward-facing triangle and a downward-facing triangle.

Here is a step-by-step guide to print a diamond by using a nested loop in C#.

First, create a new C# console application. This can be done in Visual Studio or your preferred C# environment.

using System;

namespace DiamondPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            // We'll fill in the details here.
        }
    }
}

For flexibility, let’s allow the user to determine the size of the diamond by using the below code:

Console.Write("Enter the size of the diamond: ");
int size = int.Parse(Console.ReadLine());

We’ll use nested loops. The outer loop will control the number of lines, and the inner loop will control the printing of spaces and stars (*). Below is the code:

for (int i = 0; i < size; i++)
{
    for (int j = size; j > i; j--)
    {
        Console.Write(" ");
    }
    for (int k = 0; k < (2 * i + 1); k++)
    {
        Console.Write("*");
    }
    Console.WriteLine();
}

Then, you will create the lower triangle. This is similar to the upper triangle but in reverse. Here is the code:

for (int i = size - 1; i > 0; i--)
{
    for (int j = size - i; j > 0; j--)
    {
        Console.Write(" ");
    }
    for (int k = (2 * i - 1); k > 0; k--)
    {
        Console.Write("*");
    }
    Console.WriteLine();
}

The complete C# code to print a diamond by using nested loop looks like this below:

using System;

namespace DiamondPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the size of the diamond: ");
            int size = int.Parse(Console.ReadLine());

            // Upper Triangle
            for (int i = 0; i < size; i++)
            {
                for (int j = size; j > i; j--)
                {
                    Console.Write(" ");
                }
                for (int k = 0; k < (2 * i + 1); k++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }

            // Lower Triangle
            for (int i = size - 1; i > 0; i--)
            {
                for (int j = size - i; j > 0; j--)
                {
                    Console.Write(" ");
                }
                for (int k = (2 * i - 1); k > 0; k--)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }
    }
}

Once you run the C# program, it will prompt for the size of the diamond and then display the diamond pattern on the console. You can see the screenshot below:

write a c# program to print a diamond by using nested loop

Conclusion

Here, we created a program that prints a diamond shape using nested loops in C#. In this C# tutorial, I have explained with complete code how to write a c# program to print a diamond by using nested loop.

You may also like: