program to know how many years months and hours has

hello I am new using visual studio 2010
and I doing my home work but I would like some help

this is the program that i have to write

Write a program that estimates how many years, months, weeks, days, and hours have gone by since Jan 1 1970 by calculations with the number of seconds that have gone The number of months must be less than 12, i.e., take out how many years have gone by first, then how many months are left, then weeks, etc.
Assume that all years have 365 days, and all months have 30.42 days
so far this it what I have done

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
main () {

long int now, years, months, weeks, days, secondsperYear;

now = time(NULL);
printf("Seconds = %i \n\n", now);

secondsperYear = 60*60*24*365;
years = now / secondsperYear;
//years = now % secondsperYear;
months = now / (30.42 * days);
//months = (months * (30.42 * day));
weeks = now / weeks;
//weeks = (weeks * week);

printf("Years = %i \n\n", years);
printf("Months = %i \n\n", months);
printf("Weeks = %i \n\n", weeks);

system ("pause");

}

now they told me that I have to use more variables

secondsPerYear create secondsPerHour, secondsPerDay, secondsPerWeek and secondsPerMonth. Set them the same way you worked secondsPerYear.

but i am honest i am very new with this anyhelp
will appreciate

thanks
> main () {

main() must return int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main ()
{
    const long secondsPerHour = 60 * 60 ; // EDIT

    const long hoursPerDay = 24 ;
    const long secondsPerDay = hoursPerDay * secondsPerHour ;

    const long daysPerWeek = 7 ;
    const long secondsPerWeek = daysPerWeek * secondsPerDay ;

    const double daysPerMonth = 30.42 ;
    const long secondsPerMonth = daysPerMonth * secondsPerDay ;

    const long daysPerYear = 365 ;
    const long secondsperYear = daysPerYear * secondsPerDay ;

    // ...
    // rest of your program
    // ...
}


Use %ld or %li as the format specifier to print long values:

1
2
//printf("Seconds = %i \n\n", now); 
printf( "Seconds = %ld \n\n", now );
Last edited on
thank you very much for your help
Also I think that struct tm in the ctime library has a feature kind of like that. I could be wrong though. Ps I don't recommend system( "pause" );
JL, Small typo in line 3. Donnie
Thanks, Donnie. Corrected now.
Topic archived. No new replies allowed.