The following is a short program that computes the volume of a sphere given the radius. It will compile and run, but it does not adhere to the
program style recommended in Section 2.5. Rewrite the program using the style described in the chapter for indentation, adding comments, and appropriately naming constants.
#include <iostream>
using namespace std;
int main() {
double radius, vm;
cout << "Enter radius of a sphere." << endl; cin >> radius;
vm = (4.0 / 3.0) * 3.1415 * radius * radius * radius;
cout << " The volume is " << vm << endl;
return 0;
}
The following points are modified in given program.
The following modified program code contains code indentation, adding comments, and appropriately naming constants:
Modified Program Code:
//Import file names
#include <iostream>
using namespace std;
//Program start here
int main()
{
//Double data type variables declaration
double radius, volume;
//Constant double data type variables declaration
const double PI = 3.1415;
const double NUMERATOR = 4.0;
const double DENOMINATOR = 3.0;
//Read radius of a sphere from the user
cout<<" \n Enter radius of a sphere: ";
cin>>radius;
//Computes the volume of a sphere
volume = (NUMERATOR/DENOMINATOR)* PI * radius * radius * radius;
//Display output
cout<<" The volume of a sphere is " << volume << endl;
}//End program
Output of the program code:
Enter radius of a sphere: 5.5
The volume of a sphere is 696.889