C# program to print below number triangle by entering no of lines
1
121
12321
1234321
123454321
Here's a C# program that will print the number triangle 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());
PrintNumberTriangle(numOfLines);
}
static void PrintNumberTriangle(int numOfLines)
{
for (int i = 1; i <= numOfLines; i++)
{
// Print numbers in ascending order
for (int j = 1; j <= i; j++)
{
Console.Write(j);
}
// Print numbers in descending order
for (int j = i - 1; j >= 1; 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. The first loop is responsible for printing the lines, and within each line, it prints the numbers in ascending order followed by the numbers in descending order.
When you run the program, it will ask you to enter the number of lines, and then it will print the number triangle accordingly.