I cannot seem to find the formula for this problem.

So we are supposed to convert 453456 seconds into years, days, hours, minutes, and seconds.

However, I cannot seem to get past years.

Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>

using namespace std;



 int main (){
        int secDis;
        int years;
        const int yearsSec = 31536000;
        int days;
        cout << "Please give me the time of travel in seconds.";
        cin >> secDis;
        years = secDis/yearsSec;
        days = (yearsSec%secDis) / 1440; /*idk if I am on the right track*/
        cout << "You have been traveling for: "
             << years << days;


If it is 453456 seconds it should be 0 years 5 days 5 hours 57 minutes and 36 secs.

//I hate my math skills.
Divide your initial time by how many seconds are in a year.
Divide the remainder of that by how many seconds are in a day.
Then do the same for hours and minutes.
You'll have to do two operations per variable.
First division to get the year/day/hour/etc, then modulus to get the remainder.
1
2
3
4
5
years = secDis / yearsSec ;
rem = secDis % yearsSec;
days = rem / daysSec;        //initialize daysSec as number of seconds in a day, or yearsSec / 365
rem = rem % daysSec;
//and then so on... 


That should be enough to get you going.

Edit: I changed the variable order in the code. I had it backwards before
Last edited on
I ran into another issue...

[code]
years = secDis/ yearsSec;
secRemA = secDis % yearsSec;
days = secRemA / daysSec;
secRemB = secRemA % daysSec;
hours = secRemB / hrsSec;
secRemC = secRemB % hrsSec;
[code\]
Topic archived. No new replies allowed.