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

You can find out how many seconds have elapsed since Jan 1, 1970 using the time(

ID: 3849191 • Letter: Y

Question

You can find out how many seconds have elapsed since Jan 1, 1970 using the time() function.

#include <time.h>

now = time(NULL);    // now is more than a billion seconds (which data type should you use?)

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.

Explanation / Answer

PROGRAM:

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

int main(void) {
   // your code goes here
   long now= time(NULL);
   int year,month, week, days, hours;
   /* for storing values of seconds in */
   int min=60 , hr, day, mnth, yr, wek;
   hr = min*60;
   day = hr*24;
   mnth = day*30.42;
   yr = day*365;
   wek = day*7;
  
   /* Calculating time */
   week = now/wek; // Calculating total weeks since 1970, Jan 1 as there is no measure for week in a month
   year = now / yr;
   now = now % yr;
   month = now / mnth;
   now = now % mnth;
   days = now / day;
   now = now % day;
   hours = now / hr;
   now = now / hr;
  
  
   printf("year : %d ",year);
   printf("month : %d ",month);
   printf("days : %d ",days);
   printf("hours : %d ",hours);
   printf("minutes : %d ",now);
   printf("weeks : %d ",week);

   return 0;
}

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