# Thread: Volume of a Cone Equation always equals 0

1. ## Volume of a Cone Equation always equals 0

Hey, I'm new here and I have a question that's baffling me.

I'm making an equation generator for the volume of a cone, but it won't stop equaling 0, and I have other equations that work, and I made them in the same fashion.

Code:
```#include <stdio.h>

#define PI 3.141592

int main (void)
{

/* Volume of a cone:  1/3 * PI * radius^2 * height */
double v = 0, r = 0, h = 0;

printf("Enter 2 floating-point values (radius and height) for use in volume of a cone equation: ");
scanf("%lf%lf", &r, &h);

(double) v = (double) (1/3) * PI * (double) (r * r) * (double) h;

printf ("Volume of a cone: volume_cone = 1/3 * PI * radius^2 * height = 1/3 * %lf * (%lf^2) * %lf = %lf\n", PI, r, h, v);

return (0);
}```
Thanks for any help that might come, and with any luck you guys will be seeing a lot of me as I pursue a career in computer science

2. Code:
`(double) (1/3)`
is always zero, as 1 and 3 are integers. Use 1.0 and 3.0 to make the values doubles. No cast needed in this case.

Code:
`(double) (r * r) * (double) h;`
since r and h are doubles already, there is no need to cast them.

--
Mats

3. I would change everything to float for a start.
I don't see why you need more than a float.

4. Thanks for the quick response! I fixed the 2 issues and it works just fine now

5. Originally Posted by Devolution
Thanks for any help that might come, and with any luck you guys will be seeing a lot of me as I pursue a career in computer science
[Off Topic]
Hey, you aren't associated with the OF gaming clan, are you?
[/Off Topic]

6. Originally Posted by esbo
I would change everything to float for a start.
I don't see why you need more than a float.
And the benefit of that is? Aside from saving 12 bytes of stack, I doubt you'd be able to measure the difference.

--
Mats

7. Originally Posted by kermit
[Off Topic]
Hey, you aren't associated with the OF gaming clan, are you?
[/Off Topic]

Mmm nope, I do game though.

8. IIRC that is the equation that Archimedes was most proud of, and is on his grave marker.

9. Originally Posted by matsp
And the benefit of that is? Aside from saving 12 bytes of stack, I doubt you'd be able to measure the difference.

--
Mats
Well not much except it make things look more complicated you have to worry about if the formatting is correct and then you have casts to doubles all over the case and then you can't see the woods for the trees when you get a problem.

All I am saying is it is one less thing to worry about really, I mean the first thing I thought was that he had probably done a bad conversion on the doubles somewhere, so I was lookng for some fairly obscure data conversion there when the obscure data conversion was on the the integer division. Nothing to do with run time or saving space really it just makes your program easier to check because you remove one potential source of error.

And then the formatting is lf, long float, but a long applies to an integer so you can get really confused, anyway I though you used %g to print doubles but then what do I know, I just think it is a rather confusing area which is best avoided.
Even looking on the web it seemed to come up with different interpretations, and I could not find %lf at all!!

Actually was the other problem %lf?

10. From what I've been taught so far, %d is for integer values and %lf is for floating values.

11. Originally Posted by Devolution
From what I've been taught so far, %d is for integer values and %lf is for floating values.
%lf is scanf format for doubles, for floats scanf uses %f

printf for both doubles and floats should use %f

12. Originally Posted by vart
printf for both doubles and floats should use %f
Because all variable argument functions (or other functions that hasn't got a prototype with float arguments), flaots are automagically converted to doubles.

--
Mats