C Program to Convert Days into Years, Months and Days

This a C program which converts a given number of Days in terms of years, weeks & days.

Problem Description

This program takes the number of days as input and converts in terms of years, weeks & days.

Problem Solution

1. Take the number of days as input.
2. For the number of years, divide the input by 365 and obtain its quotient.
3. For the number of weeks, divide the input by 365 and obtain its remainder.Further divide the remainder by 7(no of days in a week) and obtain its quotient.
4. For the number of days, divide the input by 365 and obtain its remainder.Further divide the remainder by 7(no of days in a week) and obtain its remainder.

Program/Source Code

Here is source code of the C program to converts a given number of Days in terms of years, weeks & days. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to convert given number of days to a measure of time given
  3.  * in years, weeks and days. For example 375 days is equal to 1 year
  4.  * 1 week and 3 days (ignore leap year)
  5.  */
  6. #include <stdio.h>
  7. #define DAYSINWEEK 7
  8.  
  9. void main()
  10. {
  11.     int ndays, year, week, days;
  12.  
  13.     printf("Enter the number of days\n");
  14.     scanf("%d", &ndays);
  15.     year = ndays / 365;
  16.     week =(ndays % 365) / DAYSINWEEK;
  17.     days =(ndays % 365) % DAYSINWEEK;
  18.     printf ("%d is equivalent to %d years, %d weeks and %d daysn",
  19.             ndays, year, week, days);
  20. }
Program Explanation

1. Take the number of days as input and store it in variable ndays.
2. For the number of years, divide the input by 365(no of days in a year) and obtain its quotient.Store this in the variable year.
3. For the number of weeks, divide the input by 365 and obtain its remainder.Further divide the remainder by 7(no of days in a week) and obtain its quotient.Store this in the variable week.
4. For the number of days, divide the input by 365 and obtain its remainder.Further divide the remainder by 7(no of days in a week) and obtain its remainder.Store this in the variable days.
5. Print the output and exit.

advertisement
advertisement
Runtime Test Cases
Case:1
Enter the number of days
29
29 is equivalent to 0 years, 4 weeks and 1 days
 
Case:2
Enter the number of days
1000
1000 is equivalent to 2 years, 38 weeks and 4 days

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.