Pyramid in Console Application

Kailash Chandra Behera | Sunday, July 05, 2020

Pyramid in Console Application provides code example to build pyramid in console application using c#. There are two code example for pyramid, the second example for reverse pyramid.

reverse pyramid in c#

Pyramid in Console Application


Pyramid Example

  class Program  
   {  
     static void Main(string[] args)  
     {  
       Console.Write("Enter the Level of pyramid :");  
                  
       int level = Convert.ToInt32(Console.ReadLine());  
                  
       for (int i = 1; i <= level; i++)  
       {  
         for (int j = 1; j <= level - i; j++)  
         {  
           Console.Write(" ");  
         }  
         for (int j = 1; j <= 2 * i - 1; j++)  
         {  
           Console.Write("*");  
         }  
         Console.WriteLine(" ");  
       }  
   
         
       Console.Read();  
     }  
   }  

Reverse Pyramid Example

  class Program  
   {  
     static void Main(string[] args)  
     {  
       Console.Write("Enter the Level of pyramid :");  
       int level = Convert.ToInt32(Console.ReadLine());  
         
       int x = 0, y = 0;  
   
       for (x = 1; x <= level; ++x)  
       {  
         // Print spaces  
         for (y = 1; y <= x; ++y)  
         {  
           Console.Write(" ");  
         }  
         // Print star/  
         for (y = 1; y <= ((level * 2) - ((2 * x) - 1)); ++y)  
         {  
           Console.Write("*");  
         }  
         // Print new line  
         Console.WriteLine(" ");  
       }  
   
       Console.Read();  
     }  
   }  

Summary

Here in the above example, we saw how to write program to print pyramid and reverse pyramid. I hope you have enjoyed a lot.

Thanks