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
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.
Dont 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
#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); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started