fork download
  1. //Devin Scheu CS1A Chapter 4, P. 220, #1
  2. //
  3. /**************************************************************
  4. *
  5. * DETERMINE MINIMUM AND MAXIMUM OF TWO NUMBERS
  6. * ____________________________________________________________
  7. * This program asks the user to enter two numbers and uses the
  8. * conditional operator to determine which is the smaller and
  9. * which is the larger.
  10. *
  11. * Computation is based on the conditional operator:
  12. * smaller = (num1 < num2) ? num1 : num2
  13. * larger = (num1 > num2) ? num1 : num2
  14. * ____________________________________________________________
  15. * INPUT
  16. * num1 : First number entered by the user (as a double)
  17. * num2 : Second number entered by the user (as a double)
  18. *
  19. * OUTPUT
  20. * smaller : The minimum value
  21. * larger : The maximum value
  22. *
  23. **************************************************************/
  24.  
  25. #include <iostream>
  26. #include <iomanip>
  27.  
  28. using namespace std;
  29.  
  30. int main () {
  31.  
  32. //Variable Declarations
  33. double num1; //INPUT - First number entered by the user (as a double)
  34. double num2; //INPUT - Second number entered by the user (as a double)
  35. double smaller; //PROCESSING - The smaller of the two numbers
  36. double larger; //PROCESSING - The larger of the two numbers
  37.  
  38. //Prompt for Input
  39. cout << "Enter the first number: ";
  40. cin >> num1;
  41. cout << "\nEnter the second number: ";
  42. cin >> num2;
  43.  
  44. //Determine Smaller and Larger Using Conditional Operator
  45. smaller = (num1 < num2) ? num1 : num2;
  46. larger = (num1 > num2) ? num1 : num2;
  47.  
  48. //Output Result
  49. cout << fixed << setprecision(2);
  50. cout << "\n";
  51. cout << left << setw(25) << "Smaller Number:" << right << setw(15) << smaller << endl;
  52. cout << left << setw(25) << "Larger Number:" << right << setw(15) << larger << endl;
  53.  
  54. } //end of main()
Success #stdin #stdout 0.01s 5284KB
stdin
200
20
stdout
Enter the first number: 
Enter the second number: 
Smaller Number:                    20.00
Larger Number:                    200.00