In this program you will read in the number of seconds and convert it to days, h
ID: 3586078 • Letter: I
Question
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. Your should then output this value as follows: Total seconds xxx Where xx 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 O For example, assume the input is: 90061Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int seconds;
cout<<"Enter total seconds: "<<endl;
cin >> seconds;
int days = seconds / (24 * 60 * 60);
seconds = seconds % (24 * 60 * 60);
int hours = seconds / (60 * 60);
seconds = seconds % (60 * 60);
int minutes = seconds / ( 60);
seconds = seconds % (60);
if(days != 0) {
cout<<days<<" days(s)"<<endl;
}
if(hours != 0) {
cout<<hours<<" hour(s)"<<endl;
}
if(minutes != 0) {
cout<<minutes<<" minute(s)"<<endl;
}
if(seconds != 0) {
cout<<seconds<<" second(s)"<<endl;
}
return 0;
}
Output:
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.