0

So i am struggling with a console application. It is required the line of chars to form a tree like pattern instead of right wing triangle. Like this(3):

  *
 ***
*****

so far i have this:

int rows = int.Parse(Console.ReadLine());

for(int i = 1; i <= rows; i++)
 {
            for(int x = 1; x <= i; x++)
            {
                Console.Write("*");
            }
            Console.WriteLine("\n");
 }
P. M.
  • 19
  • 1
  • 5

1 Answers1

1

I think you're looking for the PadLeft function. It adds spaces to your string on the left side, so you can position it correctly. Also, you need to multiply the amount of rows by 2 and increase the step size by 1. You'll get the following code:

int rows = int.Parse(Console.ReadLine()) * 2;

for (int i = 1; i <= rows; i += 2) {
  Console.Write( "".PadLeft( (rows - i) / 2) );
  for(int x = 1; x <= i; x++) {
    Console.Write("*");
  }
  Console.WriteLine();
}

Also, if you really want to make your triangle look like this, you might need to remove this line:

Console.WriteLine("\n");

... and change it to this:

Console.WriteLine();

(This will remove the unnessecary newline, WriteLine already prints a newline).

kwyntes
  • 1,045
  • 10
  • 27