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:
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:
- Write A Program To Find Numbers Above And Below The Average In C#
- Write A C# Program To Print The Multiplication Table Of A Number
- Write a C# Program to Print All Natural Numbers in Reverse Order
- Write C# Program To Calculate Power Using While & For Loop
Bijay Kumar is a renowned software engineer, accomplished author, and distinguished Microsoft Most Valuable Professional (MVP) specializing in SharePoint. With a rich professional background spanning over 15 years, Bijay has established himself as an authority in the field of information technology. He possesses unparalleled expertise in multiple programming languages and technologies such as ASP.NET, ASP.NET MVC, C#.NET, and SharePoint, which has enabled him to develop innovative and cutting-edge solutions for clients across the globe. Read more…