From 939e7a57ac8e77b212e834cc411fa9c2ac1d71a9 Mon Sep 17 00:00:00 2001 From: Divlo Date: Tue, 21 Sep 2021 15:19:16 +0200 Subject: [PATCH] feat(solutions): add `print-pyramid/cs/function` --- .../solutions/cs/function/README.md | 3 ++ .../solutions/cs/function/Solution.cs | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 challenges/print-pyramid/solutions/cs/function/README.md create mode 100644 challenges/print-pyramid/solutions/cs/function/Solution.cs diff --git a/challenges/print-pyramid/solutions/cs/function/README.md b/challenges/print-pyramid/solutions/cs/function/README.md new file mode 100644 index 0000000..a5f3222 --- /dev/null +++ b/challenges/print-pyramid/solutions/cs/function/README.md @@ -0,0 +1,3 @@ +# print-pyramid/cs/function + +Created by [@Divlo](https://github.com/Divlo) on 21 September 2021. diff --git a/challenges/print-pyramid/solutions/cs/function/Solution.cs b/challenges/print-pyramid/solutions/cs/function/Solution.cs new file mode 100644 index 0000000..c9aeb27 --- /dev/null +++ b/challenges/print-pyramid/solutions/cs/function/Solution.cs @@ -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); + } + } + } +}