settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Christmas Tree (Drawing C# Task)

+4 votes

Bai Ivan and Ani are sooooo drunk that they thought it’s Christmas so they wanted to buy a christmas tree, it doesn’t matter that it is still August. Your task is to create a christmas tree on the console as big as they want it. See the examples below to understand your task better:

Input

  • Input data is read from the console.
  • The number N stays alone at the first line.

The input data will always be valid and in the format described. There is no need to check it explicitly.

Output

  • The output data must be printed on the console.
  • You must print at the console a Christmas tree of size N following the examples below.

Constraints

  • N will be an odd integer between 5 and 49.
  • Time limit: 0.25 seconds.
  • Allowed memory: 16 MB.

Examples

Christmas tree C# - image 1

Christmas tree C# - image 2

asked in C# category by user hues
edited by user golearnweb

1 Answer

+3 votes
 
Best answer

First - have a look at my screenshot - the lines in yellow are static - the other ones are solvable with for loops and are dynamic:

Christmas tree C sharp solution

and here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class Task3
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());

        Console.WriteLine("{0}^{0}",
            new string('\'', n));//PART 1

        for (int i = 0; i < (n / 2); i++)//PART2
        {
            Console.WriteLine("{0}{1}{0}",
                new string('\'', ((n - 1) - i)),
                new string('^', (((n * 2) + 1) - (((n - 1) - i) * 2))));
        }

        for (int i = 0; i < ((n / 2) + 1); i++)//PART3
        {
            Console.WriteLine("{0}{1}{0}",
                new string('\'', ((n - 1) - i)),
                new string('^', (((n * 2) + 1) - (((n - 1) - i) * 2))));
        }

        for (int i = 0; i < ((n / 2) + 1); i++)//PART 4
        {
            Console.WriteLine("{0}{1} {1}{0}", 
                new string('\'',(n-1)),
                new string('|',1));
        }
        Console.WriteLine("{0}", 
            new string('-',((n*2)+1)));//PART 5
    }
}
answered by user john7
edited by user golearnweb
...