mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add print-pyramid/cs/function
This commit is contained in:
parent
b0d90e03bc
commit
939e7a57ac
3
challenges/print-pyramid/solutions/cs/function/README.md
Normal file
3
challenges/print-pyramid/solutions/cs/function/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# print-pyramid/cs/function
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 21 September 2021.
|
39
challenges/print-pyramid/solutions/cs/function/Solution.cs
Normal file
39
challenges/print-pyramid/solutions/cs/function/Solution.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user