1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-10-29 22:17:23 +01:00

feat(solutions): add print-pyramid/cs/function

This commit is contained in:
Divlo 2021-09-21 15:19:16 +02:00
parent b0d90e03bc
commit 939e7a57ac
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# print-pyramid/cs/function
Created by [@Divlo](https://github.com/Divlo) on 21 September 2021.

View File

@ -0,0 +1,39 @@
using System;
namespace Solution
{
class Program
{
static void Main()
{
string pyramidType = Console.ReadLine();
int pyramidHeight = int.Parse(Console.ReadLine());
PrintPyramid(pyramidType, pyramidHeight);
}
public static void PrintPyramid(string type, int height)
{
int step = type == "normal" ? 1 : height;
while ((type == "normal" && step <= height) || (type == "reverse" && step != 0))
{
int numberOfStars = (step * 2) - 1;
int totalNumberOfLocations = (height * 2) - 1;
int totalNumberOfSpaces = totalNumberOfLocations - numberOfStars;
int numberOfSpacesOnEachSide = totalNumberOfSpaces / 2;
PrintCharacter(' ', numberOfSpacesOnEachSide);
PrintCharacter('*', numberOfStars);
PrintCharacter(' ', numberOfSpacesOnEachSide);
Console.Write('\n');
step = type == "normal" ? step + 1 : step - 1;
}
}
public static void PrintCharacter(char character, int numberOfTimes)
{
for (int iteration = 0; iteration < numberOfTimes; iteration++)
{
Console.Write(character);
}
}
}
}