Software & Finance





C# Programming Loops - Left and Right Aligned Pattern





 

This program is written in C# programming language and will accept a number as input. The loops are used to build a left and right aligned pattern of *.

 

 

Source Code


using System;

using System.Collections.Generic;

using System.Text;

 

namespace Pattern

{

    class Pattern

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Program for displaying pattern of *.");

            Console.Write("Enter the maximum number of *: ");

            int n = Convert.ToInt32(Console.ReadLine());

 

            Console.WriteLine("\nPattern 1 - Left Aligned:\n");

                for (int i = 1; i <= n; i++)

                {

                      for (int j = 1; j <= i; j++)

                            Console.Write("*");

                      Console.WriteLine();

            }

 

                Console.WriteLine("\nPattern 2 - Right Aligned:\n");

                for (int i = n; i >= 1; i--)

                {

                      for(int j = 0; j < n - i; j++)

                            Console.Write(" ");

                      for (int j = 1; j <= i; j++)

                            Console.Write("*");

                      Console.WriteLine();

                }

                Console.WriteLine();

        }

    }

}

 

Output


Program for displaying pattern of *.

Enter the maximum number of *: 9

 

Pattern 1 - Left Aligned:

 

*

**

***

****

*****

******

*******

********

*********

 

Pattern 2 - Right Aligned:

 

*********

 ********

  *******

   ******

    *****

     ****

      ***

       **

        *

 

Press any key to continue . . .