//This program computes circle and sphere facts //Illustrating formatting of output //also illustrates use of input validation loop //Carol Conway #include #include #include using namespace std; int main() { const double PI = 3.1415926; double radius; double areaCircle, circumCircle, volumeSphere, areaSphere; cout << "What is the radius of your circle and sphere? "; cin >> radius; while ( radius <= 0) { cout << "Please enter a positive number for the radius "; cin >> radius; } areaCircle = PI *radius *radius; circumCircle = 2* PI * radius; volumeSphere = (4./3.)*PI * pow(radius, 3); areaSphere = 4* PI * pow(radius, 2); cout << setprecision(2) << fixed; //cout << setprecision(2); cout << "Output without using left and right defaults to the right \n"; cout << setw(20) << "Radius " << setw(15) << radius << endl; cout << setw(20) << "Area of Circle " << setw(15) << areaCircle << endl; cout << setw(20) << "Volume of Sphere " << setw(15) << volumeSphere << endl; cout << setw(20) << "Surface area Sphere " << setw(15) << areaSphere << endl; cout << "\nFields lined up perfectly with string left justified and numbers right justified\n"; cout << left << setw(20) << "Radius " << right << setw(15) << radius << endl; cout << left << setw(20) << "Area of Circle " << right << setw(15) << areaCircle << endl; cout << left << setw(20) << "Volume of Sphere " << right << setw(15) << volumeSphere << endl; cout << left << setw(20) << "Surface area Sphere " << right << setw(15) << areaSphere << endl; return 1; }