Home > DeveloperSection > Interviews > Basic star pattern code in C# language

Posted on    October-15-2016 2:24 AM

 C# C# 
Ratings:
 3 Answer(s)
  135  View(s)
Rate this:

Abhishek Srivasatava
Abhishek Srivasatava

Total Post:70

Points:350
Posted on    October-15-2016 2:24 AM

Here is the complete code for each star Pattern in C#

Pattern 1

*

**

***

****

*****

static void Main(string[] args)
        {
            int val = 5;
            int i, j;
            for (i = 0; i <= val; i++)
            {
                for (j = 0; j <= i;j++)
                {
                    Console.Write("*");
                } Console.WriteLine("");
            }
            Console.ReadLine();
        }

Pattern 2:
        *

      **

    ***

  ****

*****

static void Main(string[] args)
        {
 
                   for (int j = 10; j >0;j--)
            {
                for (int k =j ; k >=0 ;k--)
                {
                    Console.Write(" ");
                }
                for (int l = 0; l <=10-j;l++)
                {
                    Console.Write("*");
                }
                    Console.Write("\n");
            }
            Console.ReadLine();
}

Pattern 3:
Even

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

static void Main(string[] args)
        {
 
                   for (int j = 10; j >0;j--)
            {
                for (int k =j ; k >=0 ;k--)
                {
                    Console.Write(" ");
                }
                for (int l = 0; l <=10-j;l++)
                {
                    Console.Write("*");
                }
                for (int m = 0; m <=10-j; m++)
                {
                    Console.Write("*");
                }
                    Console.Write("\n");
            }
            Console.ReadLine();  
        }

Pattern 4
Odd 

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

static void Main(string[] args)
        {
 
                   for (int j = 10; j >0;j--)
            {
                for (int k =j ; k >=0 ;k--)
                {
                    Console.Write(" ");
                }
                for (int l = 0; l <=10-j;l++)
                {
                    Console.Write("*");
                }
                for (int m = 0; m < 10-j; m++)
                {
                    Console.Write("*");
                }
                    Console.Write("\n");
            }
            Console.ReadLine();  
        }

Pattern 5
*****
****
***
**
*

static void Main(string[] args)
        {
         
            int i, j;
            for (i = 10; i > 0; i--)
            {
                for (j = 0; j < i;j++)
                {
                    Console.Write("*");
                } Console.WriteLine("");
            }
            Console.ReadLine();
        }
 

Modified On Oct-20-2016 11:41:49 AM

Shikhar S.
Shikhar S.

Total Post:1

Points:5
Posted on    October-20-2016 3:24 AM

Pattern 2 is wrong as per the visual representation desired. I had tried it on the VS2013 compiler.
The code very close to the visual representation is:
            for (int i = 10; i > 0; i--)
    {
        for (int j = i; j >= i/2; j--)
        {
            Console.Write(" ");
        }
        for (int k = 0; k <= 10 - i; k++)
        {
            Console.Write("*");
        }
        Console.Write("\n");
    }
    Console.ReadLine();


Abhishek Srivasatava
Abhishek Srivasatava

Total Post:70

Points:350
Posted on    October-20-2016 11:56 AM

pattern 2  was this:
     *
   **
 ***
****

Thanks for acknowledgement .



Don't want to miss updates? Please click the below button!

Follow MindStick