While learning any programming language like C#, Java, C or C++, one of the most common exercise to learn about loops in any of these programming languages is to create star pattern program. So, in this article, I have provided various star and pyramig pattern program in C#.

star-pattern-in-c-sharp-min.png

1. Create simple pyramid star pattern using C#

In this example, we will be creating simple pyramid equilateral triangle pattern using stars ( * ) in C#.

using System;
					
public class TriangleProgram
{
	public static void Main()
	{
		    Console.WriteLine("Program for displaying pattern of *.");
            Console.Write("Enter the maximum number of * line:");
            int n = Convert.ToInt32(Console.ReadLine());
 
		    //for loop to work until i = n
            for (int i = 1; i <= n; i++)
			{
				//for loop to add space
				for (int j = 1; j <= (n - i); j++)
				{
					Console.Write(" ");
				}
                //for loop to print *, for each line 
				//k < i *2
				//so for example, if i =1, it will print star for 1 times, as loop will work  until k < 2, that is 1 time
				// when i = 2, condition will be k < 4, means it will print star for 3 times
				// and so on.
				for (int k = 1; k < i * 2; k++)
				{
					Console.Write("*");
				}
				Console.WriteLine();
			}
	}
}

Output:

Program for displaying pattern of *.
Enter the maximum number of * line:5
    *
   ***
  *****
 *******
*********

In the above code, we are using 3 for loops, one is main loop, which will loop until value of i is not equal to n

Second loop is used to include spaces before printing  star, "*".

Third loop is explained in comments of code, it is used to print "*" and create the desired pattern.

2. Printing right angle triangle pattern using * in C#

using System;
					
public class RightAngleTrianglePatternProgram
{
	public static void Main()
	{
		for (int i = 1; i <= 6; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write("*");
            }

            Console.WriteLine();
        }
       
	}
}

Output:

*
**
***
****
*****
******

This example is more simple to understand, in the above code, we are using two for loops.

Loop 1 runs until the value of i is less than or equal to 6, means we will be creating 6 rows to print star.

Loop 2 prints star's, whenever this loop is executed, it will print 1 column for 1st row, as value of i =1, and j =1, when value of i=2, it will print 2 times, as loop can run twice and so on.

3. Inverted right angled triangle

In this example, we will create inverted right angle triangle.

using System;
					
public class InvertedRightAngleTriangleProgram
{
	public static void Main()
	{
		//loop to start with 8, this time as we will print inverted triangle
		//loop to print number of rows, here 8
		for (int row = 8; row >= 1; row--)
        {
			//loop to print columns
            for (int col = 1; col <= row; col++)
            {
                Console.Write("*");
            }
            // to place cursor to next line
            Console.WriteLine();
        }
	}
}

Output:

********
*******
******
*****
****
***
**
*

I have tried to explain loops and details of this code using comments in the program.

4.  Diamond pattern using star in C#

In this program, we will divide our code in 2 parts, first part will print equilateral triangle and then second part will print invereted equilateral triangle

using System;
					
public class DiamondTriangleProgram
{
	public static void Main()
	{
		     Console.WriteLine("Program for displaying pattern of *.");
            Console.Write("Enter the maximum number of *: (Top to Middle line)");
            int n = Convert.ToInt32(Console.ReadLine());

		  //loop to print first half or equilateral triangle
            for (int i = 1; i <= n; i++)
            {
                //for loop to add space
				for (int j = 1; j <= (n - i); j++)
				{
					Console.Write(" ");
				}
                //for loop to print *, for each line 
				//k < i *2
				//so for example, if i =1, it will print star for 1 times, as loop will work  until k < 2, that is 1 time
				// when i = 2, condition will be k < 4, means it will print star for 3 times
				// and so on.
				for (int k = 1; k < i * 2; k++)
				{
					Console.Write("*");
				}
                Console.WriteLine();
            }
		  
           //loop to print inverted equilateral triangle
            for (int i = n - 1; i >= 1; i--)
            {
                 //for loop to add space
				for (int j = 1; j <= (n - i); j++)
				{
					Console.Write(" ");
				}
                //for loop to print *, for each line 
				//k < i *2
				//so for example, if i =1, it will print star for 1 times, as loop will work  until k < 2, that is 1 time
				// when i = 2, condition will be k < 4, means it will print star for 3 times
				// and so on.
				for (int k = 1; k < i * 2; k++)
				{
					Console.Write("*");
				}
                Console.WriteLine();
            }
 
            Console.WriteLine();
	}
}

Output:

Program for displaying pattern of *.
Enter the maximum number of *: (Top to Middle line)6
     *
    ***
   *****
  *******
 *********
***********
 *********
  *******
   *****
    ***
     *

As mentioned above, we have divided code into two parts and doing same thing as mentioned in 1st pattern.

You may also like to read:

Best C# programming course available online (Free OR paid)

How to read and write in Console app using C#?

How to get month name from month number in C#?