C++ Code Example: volume and surface area of a ball

The following code snippet shows the calculation of volume and surface area of a sphere. The calculations are each performed in separate functions.

#include <iostream>
#include <math.h>
using namespace std;


double getVolume(double r) {
    // Math.pow(base, exponent): Returns base raised to the power exponent
    return (4 * M_PI * pow(r, 3)) / 3;
}

double getSurfaceArea(double r) {
    return 4 * M_PI * pow(r, 2);
}

int main(){
    double r, area, volume;

    cout << "Please enter radius: ";
    cin >> r;

    area = getSurfaceArea(r);
    volume = getVolume(r);

    cout << "Volume: " << round(volume) << endl;
    cout << "Surface Area: " << round(area);

    return 0;
}
Output
Please enter radius: 2
Volume: 34
Surface Area: 50