Problems with calculating volume of a sphere.

R

rafa

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
 
E

Eric Bernard

rafa said:
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
 
A

Al Bowers

rafa said:
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;
}
 
M

Martin Ambuhl

rafa said:
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; }
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top