Velocity Reviews > Problems with calculating volume of a sphere.

# Problems with calculating volume of a sphere.

rafa
Guest
Posts: n/a

 08-09-2003
Hi I'm just learning C and I was trying to do an exercise in a book
which asks me to write a program to calculate the volume of a
sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3

and I've been trying to write that but have been unsuccesfull so far.
I tryed:
volume = 4/3 * PI * radius;
volume = volume * volume * volume;

or
volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *

And a few other variations on the algorithm. The rest of program works
fine I just don't know the proper syntax for creating this algorithm.
If someone would be kind enough to answer my question I'd also
appreciate if they could explain why the syntax needs to be the way it
is. Since right now I'm quite confused.

-rafa

Eric Bernard
Guest
Posts: n/a

 08-09-2003

"rafa" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Hi I'm just learning C and I was trying to do an exercise in a book
> which asks me to write a program to calculate the volume of a
> sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3
>
> and I've been trying to write that but have been unsuccesfull so far.
> I tryed:
> volume = 4/3 * PI * radius;
> volume = volume * volume * volume;
>
> or
> volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *
>
> And a few other variations on the algorithm. The rest of program works
> fine I just don't know the proper syntax for creating this algorithm.
> If someone would be kind enough to answer my question I'd also
> appreciate if they could explain why the syntax needs to be the way it
> is. Since right now I'm quite confused.
>
> -rafa

4/3 is an integer expression, consider volume = 4.0/3.0 * PI * radius *

Eric Bernard

Al Bowers
Guest
Posts: n/a

 08-09-2003

rafa wrote:
> Hi I'm just learning C and I was trying to do an exercise in a book
> which asks me to write a program to calculate the volume of a
> sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3
>
> and I've been trying to write that but have been unsuccesfull so far.
> I tryed:
> volume = 4/3 * PI * radius;
> volume = volume * volume * volume;
>
> or
> volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *
>

You need to force a float type division. Change 4/3 to 4.0/3.0.
The correct formula should be:

#include <stdio.h>

#define PI 3.14

int main(void)
{

printf("The volume of a sphere of radius 3cm is %.2f cu cm\n",
volume);
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x)
http://www.geocities.com/abowers822/

Martin Ambuhl
Guest
Posts: n/a

 08-09-2003
rafa wrote:

> Hi I'm just learning C and I was trying to do an exercise in a book
> which asks me to write a program to calculate the volume of a
> sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3
>
> and I've been trying to write that but have been unsuccesfull so far.
> I tryed:
> volume = 4/3 * PI * radius;
> volume = volume * volume * volume;
>
> or
> volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *

It is the radius that is cubed, not (4. * PI * radius)/3.

Also note that (4/3) is 1, but (4./3), (4/3.), (4./3.) are all floating
point values (actually doubles) with value 1.3333....
Since PI is a floating point value, (4 * PI)/3 will also have the correct
value, up to the required precision of a double or of PI, whichever is
less. The FAQ explains these things.

const double PI=3.14159265358979323846;
double cube(double x) {return x*x*x;}
double volume(double r) {return 4*PI*cube(r)/3; }

--
Martin Ambuhl