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?
Printable View
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?
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
How would I distinguish between odd and even numbers?
look at the % operator. % allows division and returns the remainder.
that is 11%2 = 1
ie
if( ( iNum % 2) != 0 )//is odd
>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:
Odd works much better:Code:* /* Uh oh */
**
****
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.Code:*
***
*****
*******
oh the even doesnt need ot be centered. im just having trouble printing the triangle.
>(^should be centered^)
>oh the even doesnt need ot be centered.
Be more specific.
>im just having trouble printing the triangle.
Think three loops:
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. :)Code:for each row
for each space
print a space
loop
for each star
print a star
loop
print a newline
loop