Please help me with the task: Output triangle using while loops

Hello, I’m working on this task:

Write a program that reads an integer count from the console. Then it should output a triangle composed of ‘#’ symbols starting from 1 in the first row and ending with count in the last row. This time, use the while loop. For example:
>4

Here is my code:


namespace WhileLoop
{
   class TriangleWhile
   {
       static void Main(string[] args)
       {
           int count = int.Parse(Console.ReadLine());
           int i = 0;
           while (i++ < count)
         {
             int j = 0;
             while (j < count)
             {
                 Console.Write("#");
                 j++;
             }
              Console.WriteLine();
           }
       }
   }
}

Can anyone please help me to solve it?

Hi @ryn97 ,

Welcome to CodeEasy!
To draw a triangle rather than a rectangle, you need to exit early on each string. Your inner loop shouldn’t iterate for the whole count, but only to ‘i’, so the condition is then:

while (j < i)
{
    ...
}

Everything else looks good to me!

Awesome! Thank you!

1 Like