fork download
  1. //Devin Scheu CS1A Chapter 4, P. 220, #4
  2. //
  3. /**************************************************************
  4. *
  5. * COMPARE AREAS OF TWO RECTANGLES
  6. * ____________________________________________________________
  7. * This program asks for the length and width of two rectangles
  8. * and determines which has the greater area, or if they are
  9. * equal.
  10. *
  11. * Computation is based on the formulas:
  12. * area1 = length1 * width1
  13. * area2 = length2 * width2
  14. * ____________________________________________________________
  15. * INPUT
  16. * length1 : Length of the first rectangle (as a double)
  17. * width1 : Width of the first rectangle (as a double)
  18. * length2 : Length of the second rectangle (as a double)
  19. * width2 : Width of the second rectangle (as a double)
  20. *
  21. * OUTPUT
  22. * message : Indicates which rectangle has the greater area or if they are equal
  23. *
  24. **************************************************************/
  25.  
  26. #include <iostream>
  27. #include <iomanip>
  28.  
  29. using namespace std;
  30.  
  31. int main () {
  32.  
  33. //Variable Declarations
  34. double length1; //INPUT - Length of the first rectangle (as a double)
  35. double width1; //INPUT - Width of the first rectangle (as a double)
  36. double length2; //INPUT - Length of the second rectangle (as a double)
  37. double width2; //INPUT - Width of the second rectangle (as a double)
  38. double area1; //PROCESSING - Area of the first rectangle
  39. double area2; //PROCESSING - Area of the second rectangle
  40. string message; //OUTPUT - Indicates which rectangle has the greater area or if they are equal
  41.  
  42. //Prompt for Input
  43. cout << "Enter the length of the first rectangle: ";
  44. cin >> length1;
  45. cout << "\nEnter the width of the first rectangle: ";
  46. cin >> width1;
  47. cout << "\nEnter the length of the second rectangle: ";
  48. cin >> length2;
  49. cout << "\nEnter the width of the second rectangle: ";
  50. cin >> width2;
  51.  
  52. //Calculate Areas
  53. area1 = length1 * width1;
  54. area2 = length2 * width2;
  55.  
  56. //Determine Comparison
  57. if (area1 > area2) {
  58. message = "The first rectangle has the greater area.";
  59. } else if (area2 > area1) {
  60. message = "The second rectangle has the greater area.";
  61. } else {
  62. message = "The areas are the same.";
  63. }
  64.  
  65. //Output Result
  66. cout << fixed << setprecision(2);
  67. cout << "\n";
  68. cout << left << setw(25) << "First Rectangle Length:" << right << setw(15) << length1 << endl;
  69. cout << left << setw(25) << "First Rectangle Width:" << right << setw(15) << width1 << endl;
  70. cout << left << setw(25) << "Second Rectangle Length:" << right << setw(15) << length2 << endl;
  71. cout << left << setw(25) << "Second Rectangle Width:" << right << setw(15) << width2 << endl;
  72. cout << left << setw(25) << "First Area:" << right << setw(15) << area1 << endl;
  73. cout << left << setw(25) << "Second Area:" << right << setw(15) << area2 << endl;
  74. cout << left << setw(25) << "Result:" << right << setw(15) << message << endl;
  75.  
  76. } //end of main()
Success #stdin #stdout 0.01s 5320KB
stdin
2
16
4
9
stdout
Enter the length of the first rectangle: 
Enter the width of the first rectangle: 
Enter the length of the second rectangle: 
Enter the width of the second rectangle: 
First Rectangle Length:             2.00
First Rectangle Width:             16.00
Second Rectangle Length:            4.00
Second Rectangle Width:             9.00
First Area:                        32.00
Second Area:                       36.00
Result:                  The second rectangle has the greater area.