Write A Program To Show Pyramidal Structure In C#

Introduction

In this blog, I am going to discuss how to show pyramid by using loop in C#.
 
Algorithm
  1. Get no. of rows that can be shown in pyramid.
  2. Loop through no. of rows.
  3. Find the centre point and add star(*).
  4. Add 2 * more than previous row in every row. 
Code:
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             int rows = 15;  
  6.             for (int i = 0; i < rows; i++)  
  7.             {  
  8.                 for (int j = rows -i - 1; j > 0; j--)  
  9.                 {  
  10.                     Console.Write(" ");  
  11.                 }  
  12.   
  13.                 for (int k = 0; k < (i +1)* 2 - 1; k++)  
  14.                 {  
  15.                     Console.Write("*");  
  16.                 }  
  17.                   
  18.                 Console.WriteLine();  
  19.             }  
  20.   
  21.             Console.ReadLine();  
  22.         }  
  23.     }  
Output

Ebook Download
View all
Learn
View all