In: Computer Science
C++ program to perform each of the area calculations in separate functions. Your program will take in the relevant information in the main (), call the correct function that makes the calculation, return the answer to the main () and then print the answer to the screen. The program will declare a variable called “choice” of type int that is initialized to 0. The program will loop while choice is not equal to 4. In the body of the loop first print a menu as follows:
1) Calculate Area of a Rectangle
2) Calculate Area of a Square
3) Calculate Area of a Triangle
4) Quit Then read in the user’s answer into the variable “choice”. Validate the user input to be an integer between 1 and 4. If the choice is 1, then call a function to perform the input and output given for the area of a rectangle, else if choice is 2 call a function to calculate the area of a square, else if choice is 3 call a function to perform the area of a triangle, else if the choice is 4 printout the message “Bye!”
Summary:
The code is given below save the file and run it .
#include<bits/stdc++.h>
using namespace std;
double areaRectangle(double length, double breadth){
double areaOfRectangle = length * breadth;
return areaOfRectangle;
}
double areaSquare(double Length){
double areaOfSquare = Length * Length;
return areaOfSquare;
}
double areaTriangle(double base, double height){
double areaOfTriangle = (base * height)/2;
return areaOfTriangle;
}
int main(){
int choice =0;
while(choice !=4){
cout << "1) Calculate Area of a Rectangle" << endl;
cout << "2) Calculate Area of a Square" << endl;
cout << "3) Calculate Area of a Triangle"<< endl;
cout << "4) Quit" << endl;
cin >> choice ;
if(choice <=0 || choice >= 5){
cout << "Please enter option between 1 to 4 " << endl;
}
if(choice ==1){
double length, breadth;
cout << "Enter the Length and Breadth of the Rectangle : "<<endl;
cin >> length >> breadth;
double area_rectangle = areaRectangle(length,breadth);
cout << "Area of the Rectangle : "<<area_rectangle << endl;
}else if(choice ==2){
double Length;
cout << "Enter the Length of the Square :"<<endl;
cin >> Length;
double area_square=areaSquare(Length);
cout<< "Area of the Square : "<< area_square << endl;
}else if(choice ==3){
double base, height;
cout << "Enter the base and height of the Triangle : " << endl;
cin >> base >> height;
double area_triangle=areaTriangle(base,height);
cout << "Area of the Triangle : " << area_triangle << endl;
}
}
cout<< "Bye!" << endl;
return 0;
}