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?
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
If cities were built like software is built, the first woodpecker to come along would level civilization.
Black Frog Studios
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
"Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
Friedrich Nietzsche
"I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
George Best
"If you are going through hell....keep going."
Winston Churchill
>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:* *** ***** *******
The information given in this message is known to work on FreeBSD 4.8 STABLE.
*The above statement is false if I was too lazy to test it.*
Please take note that I am not a technical writer, nor do I care to become one.
If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
Don't assume that I'm ever entirely serious or entirely joking.
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
The information given in this message is known to work on FreeBSD 4.8 STABLE.
*The above statement is false if I was too lazy to test it.*
Please take note that I am not a technical writer, nor do I care to become one.
If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
Don't assume that I'm ever entirely serious or entirely joking.