Introduction
In this blog, I am going to discuss how to show pyramid by using loop in C#.
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();
- }
- }


Paul WilliamsPosted Nov 10, 2017, 11:29 AM
Const int COUNT = 15;Action<int> Write = (i) => Console.WriteLine(new string(' ', (COUNT - i)) + new string('*', (i * 2) - 1));foreach (var line in Enumerable.Range(1, COUNT)) Write(line); Console.ReadLine(); // :-)