Archive for the ‘ Star ’ Category

Write a Program to Print Following Number Pattern

Input : Number of Rows

Output :

1
22
333
4444
55555

Code

        public static void PrintNumberPattern4(int rows)
        {
            for (int i = 1; i <= rows; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(i);
                }
                Console.WriteLine("");
            }
        }

Write a Program to Print Following Number Pattern

Input  : Number of Rows

Output:

1
121
12321
1234321
123454321

Code

 

        public static void PrintNumberPattern2(int rows)
        {
            int i, j, k;
            //Loop for number of Rows
            for (i = 1; i <= rows; i++)
            {
                // Loop for printing numbers in increasing order
                for (j = 1; j < 2 * i + 1; j++)
                {
                    Console.Write(j);
                    if (j >= i)
                    {
                        break;
                    }
                }
                // Loop for printing numbers in decreasing order
                for (k = j - 1; k > 0; k--)
                {
                    Console.Write(k);
                }
                Console.WriteLine("");
            }
        }

Write a Program for Printing the Stars

input : number of Rows

Output

         *
      *  *
   *  *  *
*  *  *  *
Code
        public static void PrintStar(int rows)
        {
        int temp=rows;
            for (int i = 1; i <= rows; i++)
            {
                for (int spacecounter = temp; spacecounter >= 1; spacecounter--)
                {
                    Console.Write("   ");
                }

                for (int starCounter = 1; starCounter <= i; starCounter++)
                {
                    Console.Write(" * ");
                }
                temp=temp-1;
                Console.WriteLine("");
            }
        }

Write a Program to Print Following Number Pattern

Input : Number of Rows

Output:

    1
  1 2 1
1 2 3 2 1

Code

        public static void PrintNumberPattern3(int rows)
        {
            int i, j, k, l;
            //Loop for number of Rows
            for (i = 1; i <= rows; i++)
            {
                //Loop for printing space between numbers
                for (l = 0; l < rows - i; l++)
                {
                    Console.Write("  ");
                }
                // Loop for printing numbers in increasing order
                for (j = 1; j < 2 * i + 1; j++)
                {
                    Console.Write(j + " ");
                    if (j >= i)
                    {
                        break;
                    }
                }
                // Loop for printing numbers in decreasing order
                for (k = j - 1; k > 0; k--)
                {
                    Console.Write(k + " ");
                }
                Console.WriteLine("");
            }
        }

Write the program to Print Star

Write the program to implement
*****
***
*
***
*****

 

public static void Star1(int n)
    {

      int k = n;
      int temp = n;
      Console.WriteLine();
      while (n > 0)
      {
        for (int i = 0; i < k; i++)
        {
          Console.Write("*");
        }
        
        Console.WriteLine();
        n = n - 1;
        if (n > temp / 2)
        {
          k = k - 2;
        }
        else
        {
          k = k + 2;
        }

      }
    }
 
This program will work for odd values of n

Write a Program to Print Following Number Pattern

Input : Number of Rows

Output:

1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

Code

        public static void PrintNumberPattern1(int rows)
        {

            string output = "";
            for (int i = 0; i < rows; i++)
            {
                if (i % 2 == 0)
                    output = "1 " + output;
                else
                    output = "0 " + output;

                Console.WriteLine(output);
            }
        }