fork download
  1. //Jacob Silvestre CSC5 Chapter 4, P. 220, #4
  2. //
  3. /************************************************************************************
  4. *
  5. * COMPARE RECTANGLE AREAS
  6. *____________________________________________________________________________________
  7. * This program asks the user to enter the length and width of two rectangles. The
  8. * program calculates the area of each rectangle and determines which rectangle has
  9. * the larger area, or if both rectangles have the same area.
  10. *
  11. * Rectangle Area Formula:
  12. * Area = Length x Width
  13. *____________________________________________________________________________________
  14. * INPUT
  15. * length1 : Length of Rectangle 1
  16. * width1 : Width of Rectangle 1
  17. * length2 : Length of Rectangle 2
  18. * width2 : Width of Rectangle 2
  19. *
  20. * OUTPUT
  21. * area1 : Area of Rectangle 1
  22. * area2 : Area of Rectangle 2
  23. * Displays which rectangle has the greater area
  24. *
  25. **************************************************************************************/
  26.  
  27. #include <iostream>
  28. using namespace std;
  29.  
  30. int main()
  31. {
  32. double length1; // INPUT - Length of Rectangle 1
  33. double width1; // INPUT - Width of Rectangle 1
  34. double length2; // INPUT - Length of Rectangle 2
  35. double width2; // INPUT - Width of Rectangle 2
  36. double area1; // OUTPUT - Area of Rectangle 1
  37. double area2; // OUTPUT - Area of Rectangle 2
  38.  
  39. //
  40. // Input Rectangle 1 Information
  41. //
  42.  
  43. cout << "Enter the length of Rectangle 1: ";
  44. cin >> length1;
  45.  
  46. cout << "Enter the width of Rectangle 1: ";
  47. cin >> width1;
  48.  
  49. //
  50. // Input Rectangle 2 Information
  51. //
  52.  
  53. cout << "Enter the length of Rectangle 2: ";
  54. cin >> length2;
  55.  
  56. cout << "Enter the width of Rectangle 2: ";
  57. cin >> width2;
  58.  
  59. //
  60. // Calculate Areas
  61. //
  62.  
  63. area1 = length1 * width1;
  64. area2 = length2 * width2;
  65.  
  66. //
  67. // Output Areas
  68. //
  69. cout << endl;
  70. cout << "Rectangle 1 Area: " << area1 << endl;
  71. cout << "Rectangle 2 Area: " << area2 << endl;
  72.  
  73. //
  74. // Compare Rectangle Areas
  75. //
  76.  
  77. if (area1 > area2)
  78. {
  79. cout << "Rectangle 1 has the greater area." << endl;
  80. }
  81. else if (area2 > area1)
  82. {
  83. cout << "Rectangle 2 has the greater area." << endl;
  84. }
  85. else
  86. {
  87. cout << "Both rectangles have the same area." << endl;
  88. }
  89.  
  90. return 0;
  91. }
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
Enter the length of Rectangle 1: Enter the width of Rectangle 1: Enter the length of Rectangle 2: Enter the width of Rectangle 2: 
Rectangle 1 Area: 0
Rectangle 2 Area: 0
Both rectangles have the same area.