### How to Convert Days into Years, Weeks and Days in C

If you are learning C programming, you might have encountered some problems that require you to convert a given number of days into years, weeks and days. For example, how many years, weeks and days are there in 1000 days? How about 3650 days?

In this program, I will show you how to write a simple C program that can perform this conversion for any number of days. You will also learn some basic concepts of C programming, such as variables, data types, operators, input/output and comments.

#### C program

To convert days into years, weeks and days, we need to follow these steps:

• Declare four integer variables: 'days', 'years', 'weeks' and 'rem'.
• Ask the user to enter the number of days and store it in the variable 'days'.
• Divide the number of days by 365 to get the number of years and store it in the variable 'years'.
• Find the remainder of the division by 365 to get the remaining days and store it in the variable 'rem'.
• Divide the remaining days by 7 to get the number of weeks and store it in the variable 'weeks'.
• Find the remainder of the division by 7 to get the remaining days and store it in the variable 'rem'.
• the result in the format: 'days' days = 'years' years, 'weeks' weeks and 'rem' days.

Here is the complete code:

#include <stdio.h>

int main()

{

int days, years, weeks, rem;

printf("Enter the number of days: ");

scanf("%d", &days);

years = days / 365; // calculate the number of years

rem = days % 365; // calculate the remaining days

weeks = rem / 7; // calculate the number of weeks

rem = rem % 7; // calculate the remaining days

printf("%d days = %d years, %d weeks and %d days\n", days, years, weeks, rem);

return 0;

}

#### Explanation

Let's go through each line of code and understand what it does.

1. The first line '#include <stdio.h>' is a preprocessor directive that tells the compiler to include the standard input/output header file. This file contains some functions that we will use later in our program, such as 'printf' and 'scanf'.
2. The second line 'int main()' is the main function of our program. Every C program must have a main function where the execution starts.
3. The third line '{' marks the beginning of the main function body.
4. The fourth line 'int days, years, weeks, rem;' declares four integer variables: 'days', 'years', 'weeks' and 're'. An integer variable can store a whole number, such as 1, 2 or 1000. We use these variables to store the input and output values of our program.
5. The fifth line 'printf("Enter the number of days: ");' uses the 'printf' function to print a message on the screen. The message is enclosed in double quotes and ends with a semicolon. The message asks the user to enter the number of days.
6. The sixth line 'scanf("%d", &days)' uses the 'scanf' function to read an integer value from the user and store it in the variable 'days'. The '%d' is a format specifier that tells the function what type of value to expect. The '&' is an operator that gives the address of a variable. The function needs this address to store the value in that variable. The semicolon marks the end of a statement.
7. The seventh line 'years = days / 365;' calculates the number of years by dividing the number of days by 365. The '/' is an operator that performs integer division. It discards any fractional part of the result. For example, 1000 / 365 gives 2 as an integer result. The '=' is an operator that assigns a value to a variable. It stores the result of the division in the variable 'years'.
8. The eighth line 'rem = days % 365;' calculates the remaining days by finding the remainder of the division by 365. The '%' is an operator that performs modulo operation. It gives the remainder after the division. For example, 1000 % 365 gives 270 as a remainder. The result is stored in the variable 'rem'.
9. The ninth line 'weeks = rem / 7;' calculates the number of weeks by dividing the remaining days by 7. The result is stored in the variable 'weeks'.
10. The tenth line 'rem = rem % 7;' calculates the remaining days by finding the remainder of the division by 7. The result is stored in the variable 'rem'.
11. The eleventh line 'printf("%d days = %d years, %d weeks and %d days\n, days, years, weeks, rem);' uses the 'printf' function to print the result on the screen. The function takes a format string and a list of values as arguments. The format string contains some placeholders that are replaced by the values in the list. For example, the first '%d' is replaced by the value of 'days', the second '%d' is replaced by the value of 'years', and so on. The '\n' is a special character that represents a new line. It moves the cursor to the next line after printing.
12. The twelfth line 'return 0;' ends the main function and returns a value of 0 to indicate that the program ran successfully.
13. The thirteenth line '}' marks the end of the main function body.

#### Output

Here are some sample inputs and outputs of the program:

Enter the number of days: 1000

1000 days = 2 years, 39 weeks and 4 days

Enter the number of days: 3650

3650 days = 10 years, 0 weeks and 0 days

Enter the number of days: 1

1 days = 0 years, 0 weeks and 1 days

#### Conclusion

In this program, you learned how to write a C program that can convert any number of days into years, weeks and days. You also learned some basic concepts of C programming, such as variables, data types, operators, input/output and comments.