fork download
  1. //Nathanael Schwartz CS1A Chapter 4, P. 220, #7
  2. //
  3. /*******************************************************************************
  4. *
  5. * CONVERT SECONDS TO A DIFFERENT MEASUREMENT OF TIME
  6. * ______________________________________________________________________________
  7. * This program asks the user the a certain number of seconds. Depending on the
  8. * number of seconds entered the program will convert it to minutes, hours, and
  9. * days, unless the entered time is under 60 seconds.
  10. *
  11. * Computations are Based on the Formula:
  12. * minutes = seconds / 60
  13. * hours = seconds / 3600
  14. * days = seconds / 86400
  15. * ______________________________________________________________________________
  16. * Input
  17. * seconds : the number if second entered by the user
  18. *
  19. * Output
  20. * minutes : the number of minutes for the entered seconds
  21. * hours : the number of hours for the entered seconds
  22. * days : the number of days for the eneterd seconds
  23. *******************************************************************************/
  24.  
  25. #include <iostream>
  26. #include <iomanip>
  27. using namespace std;
  28.  
  29. int main() {
  30. // Define variables
  31. float seconds; // INPUT - the number of seconds entered
  32. float minutes; // OUTPUT - the number of minutes for the entered seconds
  33. float hours; // OUTPUT - the number of hours for the entered seconds
  34. float days; // OUTPUT - the number of days for the entered seconds
  35.  
  36. // Ask the user for the amount of seconds
  37. cout << "Enter the number of seconds:\n";
  38. cin >> seconds;
  39. cout << "The number of seconds entered is " <<seconds <<" seconds\n";
  40.  
  41. // Caluclate time
  42. minutes = seconds / 60;
  43. hours = seconds / 3600;
  44. days = seconds / 86400;
  45.  
  46. // Output results
  47. cout << fixed << setprecision(2);
  48.  
  49. if (seconds < 60 && seconds >= 0)
  50. cout << "There are " <<seconds <<" seconds\n";
  51. else if (seconds >= 60 && seconds < 3600)
  52. cout << "There are " <<minutes <<" minutes in " <<seconds <<" seconds\n";
  53. else if (seconds >= 3600 && seconds < 86400)
  54. cout << "There are " <<hours <<" hours in " <<seconds << " seconds\n";
  55. else if (seconds >= 86400)
  56. cout << "There are " <<days <<" days in " <<seconds <<" seconds\n";
  57. else
  58. cout << "The number entered is invalid\n";
  59.  
  60. return 0;
  61. }
Success #stdin #stdout 0s 5288KB
stdin
-4


stdout
Enter the number of seconds:
The number of seconds entered is -4 seconds
The number entered is invalid