C# program to print star 'M' pattern by entering no of lines
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
*********** ***********
************************
Here's a C# program that prints the 'M' pattern 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 numLines = Convert.ToInt32(Console.ReadLine());
PrintMPattern(numLines);
}
static void PrintMPattern(int numLines)
{
for (int i = 1; i <= numLines; i++)
{
// Print left side of 'M'
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
// Print middle spaces
for (int k = 1; k <= (numLines - i) * 2; k++)
{
Console.Write(" ");
}
// Print right side of 'M'
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
}
}
In this program, the user is prompted to enter the number of lines. The PrintMPattern method takes the number of lines as an argument and prints the 'M' pattern accordingly. The pattern is created by printing the left side, middle spaces, and right side of the 'M' for each line. The number of stars and spaces is determined based on the line number and the total number of lines.