In this Blog, I am going to show you, how to build pyramid dynamically using C-sharp.
The end-user must enter the Height for a Pyramid.
According to the Height, the Pyramid will be printed which looks like this:
*
***
*****
*******
Using the following Solution, you can build your own Pyramid that means height of the pyramid can be changed dynamically by an end-user.
Solution
//Dynamic Approach
Console.WriteLine("Enter Height of the Pyramid (in numbers):");
int row = Convert.ToInt32(Console.ReadLine());
int col = 2 * row;
int x = col - row;
int y = x; for (int r = 0; r < row; r++)
{
for (int c = 0; c < col; c++)
{
if (c <= x && c >= y)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
x++;
y--;
Console.Write("\n");
}
Console.ReadLine();
Find the attached file for static approach in the building Pyramid.