This blog is under construction

Monday 8 July 2013

C program to convert days into years, months and weeks

Write a C program to convert days into years, months and weeks.


  #include <stdio.h>
  #define DAYSINYEAR 365
  #define DAYSINMONTH 30
  #define DAYSINWEEK 7
  int main() {
        int year, month, week, days;

        year = month = week = days = 0;
        /* get the no of days from the user */
        printf("Enter the number of days:");
        scanf("%d", &days);

        /* no of year - calculation */
        while (days >= DAYSINYEAR) {
                days = days - DAYSINYEAR;
                year++;
        }

        /* no of months - calculation */
        while (days >= DAYSINMONTH) {
                days = days - DAYSINMONTH;
                month++;
        }

        /* no of week calculation */
        while (days >= DAYSINWEEK) {
                days = days - DAYSINWEEK;
                week++;
        }

        /* print the result */
        printf("%d year, %d month, %d weeks and %d days\n",
                year, month, week, days);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of days:535    
  1 year, 5 months, 2 weeks and 6 days






See Also:

No comments:

Post a Comment