(While loop structure)
Write a small program that displays a menu with the following options:
A program that will calculate either the diameter, circumference, or area of a circle. Select your choice.
Use the switch statement. The program is to ask for the radius of a circle. Present the menu and execute the corresponding calculation.
#include <iostream>
using namespace std;
int main(){
int ch =0;
const double PI = 3.14159;
while(ch!=4){
cout<<"1. Diameter\n";
cout<<"2. Circumference\n";
cout<<"3. Area\n";
cout<<"4. Quit\n";
cout<<"Your choice: ";
cin>>ch;
float r;
if(ch>=1 && ch<=3){
cout<<"Enter the radius of a circle: ";
cin>>r;
}
switch(ch){
case 1:
cout<<"Diameter: "<<(r*2)<<"\n\n";
break;
case 2:
cout<<"Circumference: "<<(2*PI*r)<<"\n\n";
break;
case 3:
cout<<"Area: "<<(PI*r*r)<<"\n\n";
break;
case 4:
break;
default:
break;
}
}
system("pause");
return 0;
}
Comments
Leave a comment