The Program in C# Program to display Alphabet pattern ‘M’ with an asterisk is given below:
using System;
namespace AlphabetPattern
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if (j == 0 || j == rows - 1 || i == j || i == rows - 1 - j)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}
Output:
* *
** **
* * *
** **
* *
Pro-Tips💡
Here are the step by step execution of above program:
- The program starts by using the “System” namespace.
- The program defines a namespace called “AlphabetPattern”.
- The program defines a class called “Program”.
- The program defines the Main method, which is the entry point of the program.
- The program defines a variable called “rows” and assigns it a value of 5, which represents the number of rows in the pattern.
- The program uses a for loop to iterate through the rows.
- The program uses a nested for loop to iterate through the columns in each row.
- The program checks the value of
j
andi
. Ifj
is equal to 0 orj
is equal torows - 1
ori
is equal toj
ori
is equal torows - 1 - j
, it prints an asterisk to the console. Otherwise, it prints a space to the console. - The program adds a line break after each row is complete.
- The program waits for the user to press Enter before closing the console.
- The program ends by returning 0 from the Main method.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.