-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA7geometrycalculator.cpp
85 lines (75 loc) · 2.25 KB
/
A7geometrycalculator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Geometry Calculator
// By Emily Dayanghirang
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const double PI = 3.14159;
double radius, area, length, width, base, height;
int number;
// Display choices
cout << "Geometry Calculator\n\n"
<< "1. Calculate the area of a Circle\n"
<< "2. Calculate the area of a Rectangle\n"
<< "3. Calculate the area of a Triangle\n"
<< "4. Quit\n";
// Prompt user
cout << "\nEnter your choice (1-4): ";
cin >> number;
// Calculations and input validation for each of the choices
switch(number)
{
case 1:
cout << "\nEnter the circle's radius: ";
cin >> radius;
if(radius < 0)
{
cout << "\nThe radius can not be less than zero." << endl;
}
else
{
area = PI * (pow(radius, 2.0));
cout << "\nThe area is " << area << endl;
}
break;
case 2:
cout << "\nEnter the rectangle's length: ";
cin >> length;
cout << "Enter the rectangle's width: ";
cin >> width;
if(length < 0 || width < 0)
{
cout << "\nOnly enter positive values for length and width." << endl;
}
else
{
area = length * width;
cout << "\nThe area is " << area << endl;
}
break;
case 3:
cout << "Enter the length of the base: ";
cin >> base;
cout << "Enter the triangle's height: ";
cin >> height;
if(base < 0 || height < 0)
{
cout << "\nOnly enter positive values for base and height." << endl;
}
else
{
area = 0.5 * (base * height);
cout << "\nThe area is " << area << endl;
}
break;
case 4:
cout <<"Program ending." << endl;
break;
default:
cout << "The valid choices are 1 through 4. Run the"
<< "\nprogram again and select one of those."
<< endl;
}
return 0;
}