volumeof a sphere

Calculating the Volume of a Sphere in C++

To calculate the volume of a sphere in C++, you can use the formula:

V = (4/3) π r^3

Where: - V = volume of the sphere - π (pi) is a constant approximately equal to 3.14159 - r = radius of the sphere

Here's a sample C++ code to calculate the volume of a sphere using the given formula:

#include <iostream>
using namespace std;

int main() {
    float radius, volume;
    const float PI = 3.14159;

    cout << "Enter the radius of the sphere: ";
    cin >> radius;

    volume = (4.0/3.0)  PI  radius  radius  radius;

    cout << "The volume of the sphere is: " << volume << endl;

    return 0;
}

In this code: 1. We declare variables for radius and volume. 2. We define the constant PI with the value of pi. 3. We prompt the user to input the radius of the sphere. 4. We calculate the volume using the formula and display the result.

This code will calculate the volume of a sphere based on the user's input for the radius.