# Problems with calculating volume of a sphere.

Discussion in 'C Programming' started by rafa, Aug 9, 2003.

1. ### rafaGuest

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 *
radius));

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

rafa, Aug 9, 2003

2. ### Eric BernardGuest

"rafa" <> wrote in message
news...
> 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 *
> radius));
>
> 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 *
radius * radius;

Eric Bernard

Eric Bernard, Aug 9, 2003

3. ### Al BowersGuest

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 *
> radius));
>

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

#include <stdio.h>

#define PI 3.14

int main(void)
{
double volume,radius;

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

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

Al Bowers, Aug 9, 2003
4. ### Martin AmbuhlGuest

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 *
> radius));

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

Martin Ambuhl, Aug 9, 2003

## Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.