164k views
3 votes
IN C++

You are to write a program which will get the following variables from the user.
Length, Width, Height, Radius, Base.
You should also have variables for Pi as 3.14 and choice1 and choice2.
Your program will first ask the following:
Press 1 to calculate Area
Press 2 to calculate Perimeter
The choice will be saved in choice1.
You should then ask another choice:
Press 1 for Rectangle
Press 2 for Triangle
Press 3 for Circle
You have all the data required to calculate these except the Perimeter of the triangle. Only in this case will you ask the user to input lengths of side1, side2 and side3.
**You can do the shapes first and then the area/perimeter if you prefer**
Display the data neatly to the user.

User Lafual
by
5.1k points

1 Answer

2 votes

Answer:

The program is as follows:

#include <iostream>

using namespace std;

int main(){

double pi = 3.14;

int choice1, choice2;

double Length, Width, Height, Radius, Base;

double side1, side2, side3;

cout<<"Length: "; cin>>Length;

cout<<"Width: "; cin>>Width;

cout<<"Height: "; cin>>Height;

cout<<"Radius: "; cin>>Radius;

cout<<"Base: "; cin>>Base;

cout<<"Press 1 to calculate Area\\Press 2 to calculate Perimeter\\";

cin>>choice1;

if(choice1 == 1){

cout<<"Press 1 for Rectangle\\Press 2 for Triangle\\Press 3 for Circle\\";

cin>>choice2;

if(choice2 == 1){ cout<<"Area: "<<Length * Width; }

else if(choice2 == 2){ cout<<"Area: "<<0.5 * Base * Height; }

else if(choice2 == 3){ cout<<"Area: "<<pi * Radius * Radius; }

else{cout<<"Invalid choice";} }

else if(choice1 == 2){

cout<<"Press 1 for Rectangle\\Press 2 for Triangle\\Press 3 for Circle\\";

cin>>choice2;

if(choice2 == 1){ cout<<"Perimeter: "<<2 * (Length + Width); }

else if(choice2 == 2){

cout<<"Side 1: "; cin>>side1;

cout<<"Side 2: "; cin>>side2;

cout<<"Side 3: "; cin>>side3;

cout<<"Perimeter: "<<side1+side2+side3; }

else if(choice2 == 3){ cout<<"Perimeter: "<<2 * pi * Radius; }

else{cout<<"Invalid choice";} }

return 0;}

Step-by-step explanation:

This initializes pi as 3.14

double pi = 3.14;

The next three lines declare all variables

int choice1, choice2;

double Length, Width, Height, Radius, Base;

double side1, side2, side3;

The next 5 lines get inputs

cout<<"Length: "; cin>>Length;

cout<<"Width: "; cin>>Width;

cout<<"Height: "; cin>>Height;

cout<<"Radius: "; cin>>Radius;

cout<<"Base: "; cin>>Base;

This prompts the user for choice 1 (area or perimeter)

cout<<"Press 1 to calculate Area\\Press 2 to calculate Perimeter\\";

This gets input for choice 1

cin>>choice1;

If choice 1 is 1, then area is calculated

if(choice1 == 1){

This prompts the user for choice 2 (rectangle, triangle or circle)

cout<<"Press 1 for Rectangle\\Press 2 for Triangle\\Press 3 for Circle\\";

This gets input for choice 2

cin>>choice2;

Calculate area of rectangle if rectangle is selected

if(choice2 == 1){ cout<<"Area: "<<Length * Width; }

Calculate area of triangle if triangle is selected

else if(choice2 == 2){ cout<<"Area: "<<0.5 * Base * Height; }

Calculate area of circle if circle is selected

else if(choice2 == 3){ cout<<"Area: "<<pi * Radius * Radius; }

Print invalid choice for all other inputs

else{cout<<"Invalid choice";} }

If choice 1 is 2, then perimeter is calculated

else if(choice1 == 2){

This prompts the user for choice 2 (rectangle, triangle or circle)

cout<<"Press 1 for Rectangle\\Press 2 for Triangle\\Press 3 for Circle\\";

This gets input for choice 2

cin>>choice2;

Calculate perimeter of rectangle if rectangle is selected

if(choice2 == 1){ cout<<"Perimeter: "<<2 * (Length + Width); }

Calculate perimeter of triangle if triangle is selected

else if(choice2 == 2){

Get input for the three sides of the triangle

cout<<"Side 1: "; cin>>side1;

cout<<"Side 2: "; cin>>side2;

cout<<"Side 3: "; cin>>side3;

cout<<"Perimeter: "<<side1+side2+side3; }

Calculate perimeter of circle if circle is selected

else if(choice2 == 3){ cout<<"Perimeter: "<<2 * pi * Radius; }

Print invalid choice for all other inputs

else{cout<<"Invalid choice";} }

return 0;

User Kyrollos
by
4.8k points