5

I'm novice in C# and cause I request help me to implement this:

        *
        *
       ***
        *
       ***
      *****
        *
       ***
      *****
     ******* 
        *
       ***
      *****
     ******* 
    ********* 

I just had this code:

class Program
{
    static void Main(string[] args)
    {
        AnotherTriangle ob = new AnotherTriangle();
        ob.CreateTriangle();

        Console.ReadLine();
    }
}
class AnotherTriangle
{
    int n;
    string result = "*";
    public void CreateTriangle()
    {
    flag1:
        Console.Write("Please enter number of triangles of your tree: ");
        n = int.Parse(Console.ReadLine());
        Console.WriteLine();
        if (n <= 0)
        {
            Console.WriteLine("Wrong data type\n"); goto flag1;
        }
        string s = "*".PadLeft(n);
        for (int i = 0; i < n; i++)
        {
            Console.WriteLine(s);
            s = s.Substring(1) + "**";
            for (int j = 0; j < n; j++)
            {
                Console.WriteLine(s);
            }
        }
    }
}

At now I have only "tower", not equilateral triangle. Please help.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
Олег
  • 53
  • 1
  • 4
  • 2
    Please, when you get it working, look into using [`TryParse`](https://msdn.microsoft.com/en-us/library/f02979c7%28v=vs.110%29.aspx) and question why you are using `goto` – Sayse Sep 29 '15 at 11:27
  • 1
    `goto`? Really? I appreciate you are a novice, but please please never use `goto` again. It will lead you down the "bad developer" path and that can be a hard path to escape. – David Arno Sep 29 '15 at 11:36
  • 1
    Read about `goto`: [goto-is-this-bad](http://stackoverflow.com/questions/11906056/goto-is-this-bad) – Dbuggy Sep 29 '15 at 11:38
  • 1
    Never use goto - until you know why you don't ;) -- there are some rare circumstances where it can be useful. – Peter Schneider Sep 29 '15 at 11:49

1 Answers1

11
Console.WriteLine("Please enter the number of triangles in your tree: ");
int n = int.Parse(Console.ReadLine());

for (int i = 1; i <= n; i++)
{
    for (int j = 0; j < i; j++)
    {
        string branch = new String('*', j);
        Console.WriteLine(branch.PadLeft(n + 3) + "*" + branch);
    }
}

A working example.

Dmytro Shevchenko
  • 33,431
  • 6
  • 51
  • 67