C# Program to Print the pattern sequence 1 2*2 3*3*3 4*4*4*4….

C# Program to Print the pattern sequence 1 2*2 3*3*3 4*4*4*4…. -Learn how to print the pattern of following sequence containing the numbers from 1 to N and N to 1.

Input Format

The input will contain a single Integer.

Input Value

num= 5

Output Pattern

1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1

Example C# Program to Print the pattern sequence like 1 2*2 3*3*3 4*4*4*4….

class Program
{
       static void Main(string[] args)
       {
        // input value for printing the pattern.
           int num=5;

         //below for loop is printing the pattern from 1 to num value

             for (int i = 1; i <= num; i++)
              {
                for (int k = 1; k <= i; k++)
                 {
                  if(k>1&&k<=i)
                   {
                    Console.Write("*");
                   }
                   Console.Write(i);
                  if(i==k)
                   {
                    Console.Write("\n");
                   }
                 }

             }

            //below for loop is printing the pattern from num-1 to 1 value

              for (int i = num - 1; i >= 1; i--)
              {
               for (int k = 1; k <= i; k++)
               {
                if(k>1&&k<=i)
                {
                 Console.Write("*");
                 }
                 Console.Write(i);
                 if (i == k)
                 {
                  Console.Write("\n");
                 }
                }

            }
          }
}

Related Posts