In: Computer Science
Write a program using the switch statement to calculate geometric quantities. Prompt the user to enter a radius. Then present a menu of choices for quantities to be calculated from that radius:
A Area of a Circle
C Circumference of a Circle
S Surface Area of a Sphere
V Volume of a Sphere
Prompt the user to enter the character corresponding to the quantity to be calculated. Use a switch statement to handle the calculations. Use the appropriate equation in each case.
Allow for inclusion of both upper case and lower case letters and present an error message for an improper choice. Print the quantity calculated with 2 decimal digits.
When the program is working, modify it using a while or do…while loop so that the program will continue prompting the user to see if another quantity is to be calculated. For example, suppose that a radius is entered and the user specifies that the circumference is to be calculated. After the result is printed, the user will be asked if he or she wants to do another calculation. He might enter Y to continue or N to quit. Include entry of the radius as well as the switch statement in the loop.
In C++
CODE-
#include <iostream>
#include<cmath>
using namespace std;
//Main block starts
int main() {
//declaring variables
double r, p = M_PI, c = 4 / 3;
char inp, ch;
//do while loop begins
do {
//taking radius input from user
cout << "Enter a radius" << endl;
cin >> r;
//Taking input according to menu
cout << "Enter the choice according to quantities to be
calculated from that radius:" << endl;
cout << "A - Area of a Circle" << endl;
cout << "C - Circumference of a Circle" << endl;
cout << "S - Surface Area of a Sphere" << endl;
cout << "V - Volume of a Sphere" << endl;
cin >> ch;
//switch block begins
switch (ch) {
//case to calculate area of circle
case 'A':
case 'a':
printf("%.2f", p * r * r);
break;
//case to calculate circumference of circle
case 'C':
case 'c':
printf("%.2f", 2 * p * r);
break;
//case to calculate surface area of sphere
case 'S':
case 's':
printf("%.2f", 4 * p * r * r);
break;
//case to calculate Volume of sphere
case 'V':
case 'v':
printf("%.2f", c * p * r * r * r);
break;
//case for invalid input
default:
cout << "Not a valid input kindly make an appropiate choice
from the given menu" << endl;
break;
}
cout << endl << "If you wish to do any other
calculation enter Y else enter N to stop the program" <<
endl;
cin >> inp;
} while ((inp != 'n') && (inp != 'N'));
return 0;
}
CODE SCREENSHOTS-
OUTPUT-