Tuesday, 20 June 2017

Program to print pattern in C#

1. Program to print Half Pyramid using *

static void Main(string[] args)
        {
            /// Printing of star pyramid
            PrintHalfPyramid(9);


         }



/// <summary>
        /// Print half pyramid
        /// *
        /// * *
        /// * * *
        /// * * * *
        /// </summary>
        /// <param name="val"></param>
        public static void PrintHalfPyramid(int val)
        {
            for (int i = 1; i <= val; ++i)
            {
                for (int j = 1; j <= i; ++j)
                {
                    Console.Write("* ");
                }
                Console.WriteLine("\n");
            }
        }




2. Program to print Inverted Half Pyramid *

static void Main(string[] args)
        {

             //Printing of inverse star pyramid
            PrintInverseHalfPyramid(9);

           
        }


  /// <summary>
        /// Print inverse half pyramid
        /// * * *
        /// * *
        /// *
        /// </summary>
        /// <param name="val"></param>
        public static void PrintInverseHalfPyramid(int val)
        {
            for (int i = val; i >= 1; --i)
            {
                for (int j = 1; j <= i; ++j)
                {
                    Console.Write("* ");
                }
                Console.WriteLine("\n");
            }
        }




3. Programme to print full Pyramid using *

  static void Main(string[] args)
        {
           
            ////Printing full pyramid
            PrintFullPyramid(9);

        }



/// <summary>
        /// Program to Print the full pyramid
        ///    *
        ///  * * *
        ///* * * * *
        /// </summary>
        /// <param name="val"></param>
        public static void PrintFullPyramid(int val)
        {
            int k = 0;
            for (int i = 1; i <= val; ++i, k = 0)
            {
                for (int space = 1; space <= val - i; ++space)
                {
                    Console.Write("  ");
                }

                while (k != 2 * i - 1)
                {
                    Console.Write("* ");
                    ++k;
                }

                Console.WriteLine("\n");
            }
        }



4. Program to print  Square with empty diagonal.

  static void Main(string[] args)
        {

            //// Square with empty diagonal
            PrintStructure(15);
        }




/// <summary>
/// Print structure of square with empty digonal like
/*
*********
* ***** *
** *** **
*** * ***
**** ****
*** * ***
** *** **
* ***** *
*********
*/
/// </summary>
/// <param name="val"></param>
public static void PrintStructure(int val)
{
for (int i = 0; i < val; i++)
{
  for (int j = 0; j < val; j++)
    {
      if (i == 0) /// This is use  to print first line with full star
       {
         Console.Write("* ");
      }
      else if (i == val-1) /// This is use to  print last line full star
      {
         Console.Write("* ");
      }
      else if (j != val)
      {
        if ((val -1) - j == i) /// this is use to print space at end side
         {
           Console.Write("  ");
         }
         else if (i != j)
         {
           Console.Write("* ");
         }
          else
          {
           Console.Write("  "); // this is use to print space first side
          }
         }
        else
        {
          Console.Write("* ");
        }
      }
    Console.Write("\n");
  }

}



No comments:

Post a Comment