C# program to print below number diamond by entering no of lines
1
212
32123
4321234
543212345
4321234
32123
212
1
Here's a C# program that will print the number diamond pattern you described, based on the number of lines entered by the user:
using System;
class Program
{
static void Main()
{
Console.Write("Enter the number of lines: ");
int numOfLines = int.Parse(Console.ReadLine());
PrintNumberDiamond(numOfLines);
}
static void PrintNumberDiamond(int numOfLines)
{
// Print upper half of the diamond
for (int i = 1; i <= numOfLines; i++)
{
// Print spaces
for (int j = 1; j <= numOfLines - i; j++)
{
Console.Write(" ");
}
// Print numbers in descending order
for (int j = i; j >= 1; j--)
{
Console.Write(j);
}
// Print numbers in ascending order excluding the first number
for (int j = 2; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
// Print lower half of the diamond
for (int i = numOfLines - 1; i >= 1; i--)
{
// Print spaces
for (int j = 1; j <= numOfLines - i; j++)
{
Console.Write(" ");
}
// Print numbers in descending order
for (int j = i; j >= 1; j--)
{
Console.Write(j);
}
// Print numbers in ascending order excluding the first number
for (int j = 2; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
Below is the output of the above program:
In this program, the PrintNumberDiamond method takes the number of lines as a parameter and uses nested loops to print the number diamond pattern. The first loop is responsible for printing the upper half of the diamond, while the second loop prints the lower half. Within each loop, it prints the appropriate numbers and spaces based on the current line.
When you run the program, it will ask you to enter the number of lines, and then it will print the number diamond pattern accordingly.