fork download
  1. //Zachary Abdollahi CS1A Chapter 4, P. 220, #1
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * COMPARING MIN VS MAX
  6.  *______________________________________________________________________________
  7.  * This program compares two numbers that are inputted by the user. The program
  8.  * uses a conditional operator to determine which number is smaller and which
  9.  * one is larger.
  10.  *______________________________________________________________________________
  11.  * INPUT
  12.  * num1 : First number entered by the user
  13.  * num2 : Second number entered by the user
  14.  *
  15.  * OUTPUT
  16.  * smaller : The smaller of the two numbers entered
  17.  * larger : The larger of the two numbers entered
  18.  *
  19.  ******************************************************************************/
  20. #include <iostream>
  21. using namespace std;
  22. int main()
  23. {
  24. double num1; //INPUT - First number entered by the user
  25. double num2; //INPUT - Second number entered by the user
  26. double smaller; //OUTPUT - The smaller of the two numbers
  27. double larger; //OUTPUT - The larger of the two numbers
  28.  
  29. // Get input from the user
  30. cout << "Enter the first number: ";
  31. cin >> num1;
  32. cout << "Enter the second number: ";
  33. cin >> num2;
  34.  
  35. // Use the conditional operator to determine smaller and larger
  36. smaller = (num1 < num2) ? num1 : num2;
  37. larger = (num1 < num2) ? num2 : num1;
  38.  
  39. // Output result
  40. cout << "The smaller number is " << smaller << endl;
  41. cout << "The larger number is " << larger << endl;
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 5316KB
stdin
2.45 2.14

stdout
Enter the first number: Enter the second number: The smaller number is 2.14
The larger number is 2.45