In: Computer Science
1) Write a single function CalcArea( ) that has no input/output arguments. Inside the function, you will first print a “menu” listing 2 items: “rectangle”, “circle”. It prompts the user to choose an option, and then asks the user to enter the required parameters for calculating the area (i.e., the width and length of a rectangle, the radius of a circle) and then prints its area. The script simply prints an error message for an incorrect option.
Here are two examples of running it:
>> CalcArea
Menu
1. Rectangle
2. Circle
Please choose one:2
Enter the radius of the circle: 4.1
The area is 52.81.
>> CalcArea
Menu
1. Rectangle
2. Circle
Please choose one: 1
Enter the length: 4
Enter the width: 6
The area is 24.00.
#include <iostream>
using namespace std;
void CalcArea() {
int option;
cout<<"\nWelcome to CalcArea";
cout<<"\nMenu";
cout<<"\n1. Rectangle";
cout<<"\n2. Circle";
cout<<"\nPlease choose one: ";
cin>>option;
if(option==1) {
double length,width,area;
cout<<"\nEnter the length: ";
cin>>length;
cout<<"\nEnter the width: ";
cin>>width;
area=length*width;
cout<<"\nThe Area is :"<<area;
}
else if(option==2) {
double radius,area;
cout<<"\nEnter the radius of the circle: ";
cin>>radius;
area=3.14*radius*radius;
cout<<"\nThe Area is :"<<area;
}
else{
cout<<"\nIncorrect option, run again";
}
}
int main()
{
CalcArea();
return 0;
}
Thanks and regards
if you like the answer please give a thumbs up
if you have a problem, feel free to comment.