Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that estimates how many years, months, weeks, days, and hours ha

ID: 3850440 • Letter: W

Question

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 passed.

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.

Don’t use a calculator, or any web site that reports the number of seconds in a month, etc. – your C program can calculate anything that you need. Notice that this is an assignment on using arithmetic in C.

Hints. How many seconds are in a minute? hour? A day? A week? A month? A year?

here is what i have but once i get to days passed my calculations go wrong when i run the program.

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>

#define YEAR 365
#define MONTH 12
#define WEEK 4
#define DAY 30.42
#define HOUR 24
#define MINUTE 60
#define SECOND 60
int main ()
{

time_t now;
long years;
long months;
long days;
long hours;
long minutes;

now = time(NULL);
years = (now/31536000);
months = ((now/2628288)-(years*MONTH));
days = ((now/86400)-(DAY*years*MONTH));
hours = ((now/360)-(HOUR*DAY*years*MONTH));
minutes = (now/60);

printf("Years since January 1, 1970 =%ld. ", years);
printf("Months since January 1, 1970 =%ld. ", months);
printf("Days since January 1, 1970 = %ld. ", days);
printf("Hours since January 1, 1970 = %ld. ", hours);
printf("Minutes since January 1, 1970 = %ld. ", minutes);
printf("Seconds since January 1, 1970 = %ld. ", now);


return(0);
}

Explanation / Answer

Explanation:
I have calculated all the values from Years and here is the arithmetic.

Years from 1970 is 2017-1970 which is 47
Months from 1970 is Years*12 which is 47*12 = 564
Days from 1970 is years*30.42*12 which is 47*30.42*12 = 17156.88
Hours from 1970 is years*30.42*12*24 which is 47*30.42*12*24 = 411765.12
Minutes from 1970 is years*30.42*12*24*60 which is 47*12*30.42*24*60 = 24705907.20

Code:

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#define YEAR 365
#define MONTH 12
#define WEEK 4
#define DAY 30.42
#define HOUR 24
#define MINUTE 60
#define SECOND 60
int main ()
{
time_t now;
long years;
long months;
long days;
long hours;
long minutes;
now = time(NULL);
printf(" %ld ", now);
years = (now/31556926);
months = (years*MONTH);
days = (DAY*years*MONTH);
hours = (HOUR*DAY*years*MONTH);
minutes = (MINUTE*HOUR*DAY*years*MONTH);

printf("Years since January 1, 1970 =%ld. ", years);
printf("Months since January 1, 1970 =%ld. ", months);
printf("Days since January 1, 1970 = %ld. ", days);
printf("Hours since January 1, 1970 = %ld. ", hours);
printf("Minutes since January 1, 1970 = %ld. ", minutes);
printf("Seconds since January 1, 1970 = %ld. ", now);

return(0);
}

Execution and output:
Unix Terminal> ./a.out
Years since January 1, 1970 =47.
Months since January 1, 1970 =564.
Days since January 1, 1970 = 17156.
Hours since January 1, 1970 = 411765.
Minutes since January 1, 1970 = 24705907.
Seconds since January 1, 1970 = 1497921309.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote