C# program to print below number triangle by entering no of lines
1
123
12345
1234567
123456789
Here's a modified version of the 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)
{
int counter = 1;
for (int i = 1; i <= numOfLines; i++)
{
// Print spaces
for (int j = 1; j <= numOfLines - i; j++)
{
Console.Write(" ");
}
// Print numbers
for (int j = 1; j <= counter; j++)
{
Console.Write(j);
}
Console.WriteLine();
counter += 2;
}
}
}
Below is the output of the above program:
This program follows a similar structure to the previous one, but instead of printing the same number repeatedly, it prints a sequence of numbers incrementing by 1. The counter variable keeps track of the current number to be printed on each line, and it is incremented by 2 after printing each line to maintain the pattern of odd 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.