Last active
December 28, 2016 19:30
-
-
Save kkoziarski/2b84ef722a653f015fde11fe6abdce0a to your computer and use it in GitHub Desktop.
XmasTree C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* | |
*** | |
***** | |
******* | |
********* | |
*********** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace XmasTree | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.Write("Enter odd number (tree's bottom length): "); | |
int length = int.Parse(Console.ReadLine()); //9 | |
LoopsTree(length); | |
Console.WriteLine(); | |
RecursiveTree(length); | |
Console.ReadKey(); | |
} | |
static void RecursiveTree(int length, int stars = 1) | |
{ | |
int howManyStars = stars; | |
int howManySpaces = (length - stars) / 2; | |
Print(howManySpaces, " "); | |
Print(howManyStars, "*"); | |
Console.WriteLine(); | |
if (stars < length) | |
{ | |
RecursiveTree(length, stars + 2); //1, 3, 5, 7 | |
} | |
} | |
static void LoopsTree(int length) | |
{ | |
int half = length / 2; | |
for (int row = 0; row < half + 1; row++) | |
{ | |
//for (int sp = 0; sp < (half - row); sp++) | |
//{ | |
// Console.Write(" "); | |
//} | |
Print((half - row), " "); | |
//for (int star = 0; star < (row * 2 + 1); star++) | |
//{ | |
// Console.Write("*"); | |
//} | |
Print((row * 2 + 1), "*"); | |
Console.WriteLine(); | |
} | |
} | |
static void Print(int howMany, string character) | |
{ | |
for (int i = 0; i < howMany; i++) | |
{ | |
Console.Write(character); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment