Introduction
In this blog, I am going to discuss how to show pyramid by using loop in C#.
Algorithm
- Get no. of rows that can be shown in pyramid.
- Loop through no. of rows.
- Find the centre point and add star(*).
- Add 2 * more than previous row in every row.
Code:
- class Program
- {
- static void Main(string[] args)
- {
- int rows = 15;
- for (int i = 0; i < rows; i++)
- {
- for (int j = rows -i - 1; j > 0; j--)
- {
- Console.Write(" ");
- }
-
- for (int k = 0; k < (i +1)* 2 - 1; k++)
- {
- Console.Write("*");
- }
-
- Console.WriteLine();
- }
-
- Console.ReadLine();
- }
- }
Output