(c++) (CodeBlocks) Write a program that uses a loop-driven menu that displays as
ID: 3833315 • Letter: #
Question
(c++) (CodeBlocks)
Write a program that uses a loop-driven menu that displays as:
Area Calculator
1. Circle
2. Rectangle
3. Triangle
4. Exit the calculator
If the user enters 1, prompt the user to enter the radius of the circle. Use the formula area = pi * r squared, (use 3.14159 for pi) Display:
The area of the circle is <calculated value>
If the user enters 2, prompt the user to enter the length and width of the rectangle. Use the formula area = length * width. Display:
The area of the rectangle is <calculated value>
If the user enters 3, prompt the user to enter the base and height of the triangle. Use the formula area = base * height * .5. Display:
The area of the triangle is <calculated value>
Run the program 3 times with the test data:
Test data:
first run:
area of a circle, radius 8
second run:
area of a rectangle, length 16, width 8
third run:
area of a triangle, base 8, height 16
Capture the output of your program running into your source code. Submit your source code file.
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
cout << "Area Calculator" << endl;
while(true)
{
cout << "1. Circle" << endl;
cout << "2. Rectangle"<< endl;
cout << "3. Triangle"<< endl;
cout << "4. Exit the calculator" << endl;
int choice;
cin >> choice;
switch(choice)
{
case 1:
cout << "Enter radius: ";
int radius;
cin >> radius;
cout << "area of a cricle, radius ";
cout << "area of a cricle, radius " << radius << " is " << 3.14*radius*radius << endl;
break;
case 2:
cout << "Enter length width : ";
int length, width;
cin >> length >> width;
cout << "area of a rectangle, length " << length << ", width " << width;
cout << " is " << length*width << endl;
break;
case 3:
cout << "Enter base height : ";
int base, height;
cin >> base >> height;
cout << "area of a triangle, base " << base << ", height " << height;
cout << " is " << (base*height)/2.0 << endl;
break;
case 4:
return 0;
default:
cout << "Please choose from given menu!" << endl;
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.