formula!



hi,

the formula = 1/3PI x Radius^2 x Height

in C++ program

I wrote it like this = (((1/3)*pi)*(pow(r,2))*h)

pi,r,and h declared as float

is it correct ?

because in the output screen i got 0 as a value ,I feel that it's wrong!!

help me :)
1/3 is an integer division ( with a truncated result ). Use 1.0 / 3 to have a double as result.
Also you don't need all those parentheses ( C++ is not Lisp :^)
1.0 / 3 * pi * pow(r,2) * h would be fine
thanks :>

I have the similar problem with this.
When I will convert a Celcius degree onto Fahrenheit.
Since I only know that the formula is
fahrenheit = 9/5 * celcius + 32

so I use that formula.
In this case, celcius is double type.
But it is failed.

So I change the order of writing the code, like this
fahrenheit = 9 * celcius/5 + 32
and it works.

The point is we need to confirm the type of operand such that satisfy our need.
I'm sorry about my English.
Just a suggestion. It is a little overkill to call the pow function just to compute r2. You only need to multiply r by itself.
 
1.0 / 3 * pi * r * r * h
thanks :)
Topic archived. No new replies allowed.