Write a complete program that prompts the user for the radius of a sphere, and calculates and
prints the volume of that sphere. Use a function sphereVolume that returns the result of the
following expression: (4/3) × π × r3 which is equivalent to
( 4.0 / 3.0 ) * 3.14159 * pow( radius, 3 ) in C++ code.
Sample output
Enter the length of the radius of your sphere: 2
#include <iostream>
#include <cmath>
using namespace std;
double sphereVolume(double radius) {
double volume;
volume = (4.0/3.0) * 3.14159 * pow(radius, 3);
return volume;
}
int main() {
double radius, volume;
cout << "Enter the length of the radius of you spger: ";
cin >> radius;
volume = sphereVolume(radius);
cout << "The volume of your sphere is " << volume << endl;
return 0;
}
Comments
Leave a comment