fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7.  
  8. const double PI = 3.14159;
  9. double num, area, radius, base, length, width, height;
  10.  
  11. cout << "Geometry Calculator\n";
  12. cout << " 1. Calculate the Area of a Circle\n";
  13. cout << " 2. Calculate the Area of a Rectangle\n";
  14. cout << " 3. Calculate the Area of a Triangle\n";
  15. cout << " 4. Quit\n\n";
  16. cout << " Enter your choice (1-4): ";
  17.  
  18. cin >> num;
  19.  
  20. if (num == 1)
  21.  
  22. {
  23. cout << "Enter the length of the radius: ";
  24. cin >> radius;
  25.  
  26. if (radius < 0)
  27. {
  28. cout << "Enter values greater than 0.";
  29. exit (0);
  30. }
  31.  
  32. else
  33. {
  34. area = PI * pow (radius, 2.0);
  35.  
  36. cout << "The area of the circle is " << area << " square units.\n";
  37. }
  38. }
  39.  
  40. else if (num == 2)
  41. {
  42. cout << "Enter the length of the rectangle: ";
  43. cin >> length;
  44. if (length < 0)
  45. {
  46. cout << "Error. Enter values greater than 0.";
  47. exit (0);
  48. }
  49.  
  50. else
  51. {
  52. cout << "Enter the width of the rectangle: ";
  53. cin >> width;
  54.  
  55. if (width < 0)
  56. {
  57. cout << "Error. Enter values greater than 0.";
  58. exit (0);
  59. }
  60.  
  61. else
  62. {
  63. area = length * width;
  64.  
  65. cout << "The area of the rectangle is " << area <<
  66. " square units.";
  67. }
  68. }
  69.  
  70. }
  71.  
  72. else if (num == 3)
  73. {
  74. cout << "Enter the length of the triangle's base: ";
  75. cin >> base;
  76.  
  77.  
  78. if (base < 0)
  79. {
  80. cout << "Error. Enter values greater than 0.";
  81. exit (0);
  82. }
  83.  
  84. else
  85. {
  86. cout << "Enter the triangle's height: ";
  87. cin >> height;
  88.  
  89. if (height < 0)
  90. {
  91. cout << "Error. Enter values greater than 0.";
  92. exit (0);
  93. }
  94.  
  95. else
  96. {
  97. area = base * height * 0.5;
  98.  
  99. cout << "The area of the triangle is " << area <<
  100. " square units.";
  101. }
  102. }
  103. }
  104. else if (num == 4)
  105. {
  106.  
  107. cout << "Thank you. Goodbye.";
  108. exit (0);
  109. }
  110. }
Success #stdin #stdout 0s 4504KB
stdin
Standard input is empty
stdout
Geometry Calculator
    1. Calculate the Area of a Circle
    2. Calculate the Area of a Rectangle
    3. Calculate the Area of a Triangle
    4. Quit

    Enter your choice (1-4):