fork download
  1. // Cenyao Huang CS1A Homework 4, p. 220, #4
  2.  
  3. /************************************************************************************
  4. * DETERMINE WHICH RECTANGLE AREA IS GREATER
  5. *
  6. * This program determines which of two triangle areas is greater after their length
  7. * and widths are inputted.
  8. *
  9. * This program will use the formula:
  10. * Area = length * width
  11. *
  12. * Input
  13. * length1 : the value of the length of the first rectangle
  14. * width1 : the value of the width of the first rectangle
  15. * length2: the value of the length of the second rectangle
  16. * width2 : the value of the width of the second rectangle
  17. * Area1 : the value of the area of the first rectangle
  18. * Area2 : the value of the area of the second rectangle
  19. *
  20. * Output
  21. * A message that shows which rectangle is greater, or if the input is invalid
  22. *
  23. *
  24. ************************************************************************************/
  25. #include <iostream>
  26. using namespace std;
  27.  
  28. int main() {
  29. double length1, width1, length2, width2, Area1, Area2; // variable assignment
  30.  
  31. // Ask and display length, width, and area of first rectangle
  32. cout << "Please enter the length and width the first rectangle: ";
  33. cin >> length1 >> width1;
  34. cout << length1 << " " << width1 << endl;
  35. Area1 = length1 * width1;
  36. cout << "The area of the first rectangle is: " << Area1 << endl << endl;
  37.  
  38. // Ask and display length, width, and area of second triangle
  39. cout << "Please enter the length and width of the second triangle: ";
  40. cin >> length2 >> width2;
  41. cout << length2 << " " << width2 << endl;
  42. Area2 = length2 * width2;
  43. cout << "The area of the second rectangle is: " << Area2 << endl << endl;
  44.  
  45. // determine and display the greater area
  46. if (Area1 > Area2)
  47. cout << "The first rectangle has a greater area.";
  48. else if (Area2 > Area1)
  49. cout << "The second rectangle has a greater area.";
  50. else
  51. cout << "Invalid input. Please try again.";
  52.  
  53. return 0;
  54. }
Success #stdin #stdout 0.01s 5324KB
stdin
20 20 30 30
stdout
Please enter the length and width the first rectangle: 20 20
The area of the first rectangle is: 400

Please enter the length and width of the second triangle: 30 30
The area of the second rectangle is: 900

The second rectangle has a greater area.