# Need help drawing triangles (C)

• 09-30-2003
Belvedeer84
Need help drawing triangles (C)
I need to allow the user to input length n and draw a triangle as shown below based on the length n.

n = 7 (odd number)
*
***
*****
*******
(^should be centered^)

n= 4 (even number)
*
**
***
****

Can anybody help me at least get started?
• 09-30-2003
sean345
You can get the users input using scanf. Then I would use a for loop to print out the triangles. Give it a try and if you still need help post the code you have created so far.

- Sean
• 09-30-2003
Belvedeer84
How would I distinguish between odd and even numbers?
• 09-30-2003
novacain
look at the % operator. % allows division and returns the remainder.

that is 11%2 = 1

ie

if( ( iNum % 2) != 0 )//is odd
• 10-02-2003
twm
>How would I distinguish between odd and even numbers?
That's not nearly as interesting as how you would get an even number well centered:
Code:

```  * /* Uh oh */  ** ****```
Odd works much better:
Code:

```  *   ***  ***** *******```
Using an ASCII display you can't get an even numbered pyramid to center properly. In this case I would make sure that the number is always odd by either requiring an odd number to be input, or making it odd by adding or subtracting one if it's even.
• 10-02-2003
Belvedeer84
oh the even doesnt need ot be centered. im just having trouble printing the triangle.
• 10-02-2003
twm
>(^should be centered^)
>oh the even doesnt need ot be centered.
Be more specific.

>im just having trouble printing the triangle.
Think three loops:
Code:

```for each row   for each space     print a space   loop   for each star     print a star   loop   print a newline loop```
Naturally you'll need a variable with the number of spaces to print that you decrement and a variable with the number of starts to print that gets incremented. This should get you started on experimenting, have fun. :)