fork download
  1. // Marvyn De La Torre CS1A Chapter 4, P.225, #21
  2.  
  3. /**************************************************************
  4. * Calculate Geometric Area
  5. *
  6. * This program calculates the area of a circle, rectangle, or
  7. * triangle based on the selected geometric shape.
  8. *
  9. * Input:
  10. * Menu selection = choice
  11. * Radius = radius
  12. * Length = length
  13. * Width = width
  14. * Base = base
  15. * Height = height
  16. *
  17. * Output:
  18. * Area = area
  19. **************************************************************/
  20.  
  21. #include <iostream>
  22. #include <iomanip>
  23. using namespace std;
  24.  
  25. int main()
  26. {
  27. int choice;
  28. double area, radius, length, width, base, height;
  29.  
  30. cout << "Geometry Calculator" << endl;
  31. cout << "1. Calculate the Area of a Circle" << endl;
  32. cout << "2. Calculate the Area of a Rectangle" << endl;
  33. cout << "3. Calculate the Area of a Triangle" << endl;
  34. cout << "4. Quit" << endl;
  35. cout << "Enter your choice: ";
  36. cin >> choice;
  37.  
  38. cout << fixed << showpoint << setprecision(2);
  39.  
  40. switch (choice)
  41. {
  42. case 1:
  43. cout << "Enter the radius: ";
  44. cin >> radius;
  45.  
  46. if (radius < 0)
  47. cout << "Radius cannot be negative." << endl;
  48. else
  49. {
  50. area = 3.14159 * radius * radius;
  51. cout << "Area: " << area << endl;
  52. }
  53. break;
  54.  
  55. case 2:
  56. cout << "Enter the length: ";
  57. cin >> length;
  58.  
  59. cout << "Enter the width: ";
  60. cin >> width;
  61.  
  62. if (length < 0 || width < 0)
  63. cout << "Length and width cannot be negative." << endl;
  64. else
  65. {
  66. area = length * width;
  67. cout << "Area: " << area << endl;
  68. }
  69. break;
  70.  
  71. case 3:
  72. cout << "Enter the base: ";
  73. cin >> base;
  74.  
  75. cout << "Enter the height: ";
  76. cin >> height;
  77.  
  78. if (base < 0 || height < 0)
  79. cout << "Base and height cannot be negative." << endl;
  80. else
  81. {
  82. area = base * height * 0.5;
  83. cout << "Area: " << area << endl;
  84. }
  85. break;
  86.  
  87. case 4:
  88. cout << "Program ended." << endl;
  89. break;
  90.  
  91. default:
  92. cout << "Invalid choice." << endl;
  93. }
  94.  
  95. return 0;
  96. }
Success #stdin #stdout 0s 5296KB
stdin
2 10 20
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: Enter the length: Enter the width: Area: 200.00