fork download
  1. // Chapter 4 - Programming Challenge #4
  2. // Areas of Rectangles
  3. // This program asks the user for the length and width
  4. // of two rectangles. It calculates the area of each
  5. // rectangle and determines which rectangle has the
  6. // greater area, or if both areas are equal.
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13. // Variables for rectangle 1
  14. double length1, width1;
  15.  
  16. // Variables for rectangle 2
  17. double length2, width2;
  18.  
  19. // Variables to store the calculated areas
  20. double area1, area2;
  21.  
  22. // Get the dimensions of rectangle 1
  23. cout << "Enter the length of rectangle 1: ";
  24. cin >> length1;
  25.  
  26. cout << "Enter the width of rectangle 1: ";
  27. cin >> width1;
  28.  
  29. // Get the dimensions of rectangle 2
  30. cout << "Enter the length of rectangle 2: ";
  31. cin >> length2;
  32.  
  33. cout << "Enter the width of rectangle 2: ";
  34. cin >> width2;
  35.  
  36. // Calculate the area of each rectangle
  37. area1 = length1 * width1;
  38. area2 = length2 * width2;
  39.  
  40. // Compare the two areas
  41. if (area1 > area2)
  42. {
  43. cout << "Rectangle 1 has the greater area." << endl;
  44. }
  45. else if (area2 > area1)
  46. {
  47. cout << "Rectangle 2 has the greater area." << endl;
  48. }
  49. else
  50. {
  51. cout << "Both rectangles have the same area." << endl;
  52. }
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0s 5320KB
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: Both rectangles have the same area.