fork download
  1. /*******************************************************************************
  2. * calculate area of circle/rectangle/triangle
  3. *_______________________________________________________________________________
  4. *calculates area of a shape chosen by input
  5. *_______________________________________________________________________________
  6. *INPUT
  7. * if 1 (area of circle), radius
  8. * if 2 (area of rectangle), length, width
  9. * if 3 (area of triangle), base, height
  10. * if 4, quit
  11. *OUTPUT (AREA)
  12. * if 1, pi * radius * radius
  13. * if 2, length * width
  14. * if 3, (base * height) * 0.5
  15. * if 4, "goodbye"
  16. *******************************************************************************/
  17.  
  18. #include <iostream>
  19. using namespace std;
  20.  
  21. int main() {
  22. int choice;
  23. double radius;
  24. double length;
  25. double width;
  26. double base;
  27. double height;
  28. double area;
  29. const double pi = 3.14159;
  30.  
  31. cout << "choose the shape you want to calculate the area of\n";
  32. cout << "1. circle\n" << "2. rectangle\n" << "3. triangle\n" << "4. quit\n";
  33. cin >> choice;
  34.  
  35. if (choice==1) {
  36. cout << " \n" << "you have selected circle\n" << "enter radius\n";
  37. cin >> radius;
  38. if (radius < 0) {
  39. cout << "radius can not be negative.\n";
  40. } else {
  41. area = radius * radius * pi;
  42. cout << "area = " << area << endl;
  43. }
  44. } else if (choice==2) {
  45. cout << " \n" << "you have selected rectangle\n";
  46. cout << "enter length\n";
  47. cin >> length;
  48. cout << "enter width\n";
  49. cin >> width;
  50. cin >> radius;
  51. if (length < 0 || width < 0) {
  52. cout << "length and width can not be negatives.\n";
  53. } else {
  54. area = length * width;
  55. cout << "area = " << area << endl;
  56. }
  57. } else if (choice==3) {
  58. cout << " \n" << "you have selected triangle\n";
  59. cout << "enter base\n";
  60. cin >> base;
  61. cout << "enter height\n";
  62. cin >> height;
  63. if (base < 0 || height < 0) {
  64. cout << "base and height can not be negatives.\n";
  65. } else {
  66. area = base * height * 0.5;
  67.  
  68. cout << "area = " << area << endl;
  69. }
  70. } else if (choice==4) {
  71. cout << " \n"<< "you decided to quit, goodbye!\n";
  72. }
  73.  
  74.  
  75. return 0;
  76. }
Success #stdin #stdout 0.01s 5260KB
stdin
4
stdout
choose the shape you want to calculate the area of
1. circle
2. rectangle
3. triangle
4. quit
 
you decided to quit, goodbye!