C Program to convert days into years and weeks format

April 16, 2022, Learn eTutorial

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

How to change the days to years and weeks format in C?

In this c program, we need to change the number of days into the format of years, weeks, and days. That means if the user has input 372 days, the output of the c program should be 1 year and 1 week like that. Here we need to convert the number of days into years and the remaining days into weeks and days.

In this c program, we use the mod operator to find the remainder of the year and week. First, we define the number of days in a week as 7. After accepting the user input, for finding years, we divide the number of days by 365. To calculate the remaining weeks, use mod the number with 365 and divide that remainder with 7. Like that, we have to find out the days also. Step by step procedure that happens in this program is:

  • 1. Input the number of variables and placed it in variable 'n' days.
  • 2. To get the number of Years, Divide the input by 365 and store the Quotient as the Year.
  • 3. For calculating the number of Weeks, Divide input by 365, and obtain the Remainder. Divide the Remainder again by 7(number of days in a week) and store Quotient in variable 'week'.
  • 4. To get the number of Days, Divide the input by 365 and obtain the Remainder. Again Divide the Remainder with 7(number of days in a week) and get the Remainder. Store the value in the variable Days.
  • 5. Print the Output and then Exit.

What is  #define explain the syntax?

#define is used to Declare the constant values in our program. Here we define the Constant DAYINWEEK; its value is 7. We can use the Constant 'DAYINWEEK' in this example to calculate the Week, Days, etc.

The Syntax for defining a Constant is:

#define ConstantName Value

  • 'ConstantName' is the name of the constant Variable.
  •  Value is the value of the Variable.


STEP 1: Include the Header files to use the built-in Functions in the C program.

STEP 2: Declare the variables 'ndays, Year, week, days' as integer and Define DAYSINWEEK 7.'

STEP 3: Read the number of Days into 'ndays'.

STEP 4: Calculate Year as 'ndays/365'.

STEP 5: Calculate Week as '(ndays % 365)/DAYSINWEEK'.

STEP 6: Calculate Days as '(ndays65) % DAYSINWEEK'. 

STEP 7: Display 'ndays' is equivalent to 'year' and 'week' and 'days' using the printf function.

C Source Code

                                          #include <stdio.h>
#define DAYSINWEEK 7

void main()
  int ndays, year, week, days;
  printf("Enter the number of days\n");
  scanf("%d", & ndays);
  year = ndays / 365;
  week = (ndays % 365) / DAYSINWEEK; /* calculating the years, weeks and days  */
  days = (ndays% 365) % DAYSINWEEK;
  printf("%d is equivalent to %d years, %d weeks and %d days\n", ndays, year, week, days);


Enter the number of days
 375 is equivalent to 1 year, 1 week and 3 days

 Enter the number of days
 423 is equivalent to 1 year, 8 weeks, and 2 days

 Enter the number of days
 1497 is equivalent to 4 years, 5 weeks and 2 days