533,525 views
15 votes
15 votes
In this program you will read in the number of seconds and convert it to days, hours, minutes and remaining seconds.

Your program will make use of long long int variables for all calculations. Note: the use of long long int requires that you have C++11 support. You should have this automatically if you are using a newer version of Visual Studio. The support is there for GCC as well, but you may need the -std=c++11 or -std=c++0x compiler flag.
You first need to read in the total number of seconds. The prompt for the read is: Enter seconds
Your should then output this value as follows. Note that this is written out after you have read in seconds.
Total seconds: xxx
Where xxx is the number of seconds you read in.
If this value is 0 or less you need to output the message Total seconds must be greater than zero
For example, assume the input is:
-102
The output will be as follows
Total seconds: -102
Total seconds must be greater than zero
If the number is more than zero you need to calculate the number of days, hours, minutes and seconds. The value for hours should be after you have subtracted off the number of days. The value for minutes will be the number of minutes after you have subtracted the days and hours. Seconds will be the remainder after you have subtracted the days, hours and minutes.
You should only output the days if the number of days is more than 0. You should only output the hours if the number of hours is more than 0. You should only output the minutes if the number of minutes is more than 0 and you should only output the seconds if the number of seconds is greater than 0.
For example, assume the input is:
90061
The output will be as follows
Total seconds: 90061
1 day (s)
1 hour (s)
1 minute (s)
1 second (s)
The following input:
36011
will result in the output:
Total seconds: 36011
10 hours(s)
11 seconds (s)
The output must contain the above headings, messages, formatting exactly as shown.

User Karol Pisarzewski
by
2.3k points

1 Answer

21 votes
21 votes

Answer:

In C++

#include <iostream>

using namespace std;

int main(){

int days, hours, minutes, seconds;

cout<<"Total seconds: ";

cin>>seconds;

if(seconds>0){

days = seconds/86400;

seconds%=86400; ;

hours = seconds/3600;

seconds%=3600;

minutes = seconds/60;

seconds %=60;

cout<<days<<" days "<<hours<<" hours "<<minutes<<" minutes "<<seconds<<" seconds";}

else{ cout<<"Total seconds must be greater than zero"; }

return 0;}

Step-by-step explanation:

Declare all variables as integer

int days, hours, minutes, seconds;

Prompt for input

cout<<"Total seconds: ";

Get user inpur for seconds

cin>>seconds;

If input is greater than 0

if(seconds>0){

Calculate the number of days

days = seconds/86400;

Get the remaining seconds

seconds%=86400; ;

Calculate the number of hours

hours = seconds/3600;

Get the remaining seconds

seconds%=3600;

Calculate the number of minutes

minutes = seconds/60;

Get the remaining seconds

seconds %=60;

Print the required output

cout<<days<<" days "<<hours<<" hours "<<minutes<<" minutes "<<seconds<<" seconds";}

If seconds is less than 1, then it is invalid

else{ cout<<"Total seconds must be greater than zero"; }

User Shijinmon Pallikal
by
3.0k points