C# program to print below number triangle by entering no of lines
1
212
32123
4321234
543212345
Here's a C# program that will print the number triangle 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());
PrintNumberTriangle(numOfLines);
}
static void PrintNumberTriangle(int numOfLines)
{
for (int i = 1; i <= numOfLines; i++)
{
// Print spaces
for (int j = 1; j <= numOfLines - i; j++)
{
Console.Write(" ");
}
// Print decreasing numbers
for (int j = i; j >= 1; j--)
{
Console.Write(j);
}
// Print increasing numbers
for (int j = 2; j <= i; j++)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
Below is the output of the above program:
In this program, the PrintNumberTriangle method takes the number of lines as a parameter and uses nested loops to iterate through the lines and print the appropriate numbers and spaces. The first loop is responsible for printing the lines, the second loop prints the decreasing numbers, and the third loop prints the increasing numbers.
When you run the program, it will ask you to enter the number of lines, and then it will print the number triangle accordingly.