Write a program that inputs an integer that represents a length of time in seconds. The program should then output the number of hours, minutes, and seconds that corresponds to that number of seconds. For example, if the user inputs 50391 total seconds then the program should output 13 hours, 59 minutes, and 51 seconds.
#include <iostream>
using namespace std;
int main( )
{
const unsigned int SECONDS= 60;
const unsigned int MINUTES= 60;
const unsigned int HOUR= 24;
const unsigned int DAY= 7;
unsigned long totalSeconds;
unsigned long weeks, dd, hr, minutes, seconds;
//Read totalSeconds
cout << "Enter total number of seconds: ";
cin >> totalSeconds;
//Calcualte the number of hours, minutes,
//and seconds
seconds = totalSeconds;
minutes = seconds / SECONDS;
seconds %= SECONDS;
hr = minutes / MINUTES;
minutes %= MINUTES;
dd = hr / HOUR;
hr %= HOUR;
weeks = dd / DAY;
dd %= DAY;
//Display output
cout << totalSeconds << " seconds is ";
if (weeks > 0) cout << weeks << " weeks ";
if (dd > 0) cout << dd << " days ";
if (hr > 0) cout << hr << " hours ";
if (minutes > 0) cout << minutes << " minutes ";
if (seconds > 0) cout << seconds << " seconds "<<endl;
system("PAUSE");
return 0;
}
OUTPUT: