Skip to content

Instantly share code, notes, and snippets.

@kkoziarski
Last active December 28, 2016 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkoziarski/2b84ef722a653f015fde11fe6abdce0a to your computer and use it in GitHub Desktop.
Save kkoziarski/2b84ef722a653f015fde11fe6abdce0a to your computer and use it in GitHub Desktop.
XmasTree C#
*
***
*****
*******
*********
***********
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