How to draw a triangle

I am assigned to draw a triangle but I am unsure what to do. Could any one help me?

My triangle should look like this :

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

Last edited on

____*           4, 1
___***          3, 3
__*****         2, 5
_*******        1, 7
*********       0, 9

The underscores represent spaces, and the two numbers represent the number of spaces and asterisks, respectively.

We can see that the number of asterisks start at 1 and increase by 2 every line. It can be represented by a = 2n - 1, where a is the number of asterisks and n is the current line, with the first line being 1. Thus, to get how many asterisks we need to print, all we need is the current line number.

This is a triangle with a total of 5 lines. Note that the first line has 4 spaces, second line has 3 spaces, third line has 2 spaces, etc. See the pattern? Thus, to get how many spaces you would need to print, subtract the total amount of lines from the current line number. Here's a formula: spaces = total_lines - n

Hopefully, you can apply this logic to code, if you can't feel free to post what you've tried so far and I'll help you.
Topic archived. No new replies allowed.