# Thread: Need help drawing triangles (C)

1. ## 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?

2. 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

3. How would I distinguish between odd and even numbers?

4. look at the % operator. % allows division and returns the remainder.

that is 11%2 = 1

ie

if( ( iNum % 2) != 0 )//is odd

5. >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.

6. oh the even doesnt need ot be centered. im just having trouble printing the triangle.

7. >(^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.