Given the number of days, the task is to convert the given number of days into years, weeks and days.
Let us assume the number of days in a year = 365
Number of years = (number of days)/365
Explanation-: The number of years will be divided by giving The quotient of a given number of days and 365
number of weeks = (number of days % 365) / 7
Explanation-: The number of weeks will be obtained by collecting the remainder and dividing the number of days by 365 , divided by the number of days in the week, 7.
Number of days = (Number of days % 365) % 7
Explanation-: The number of days is the remainder obtained by dividing the number of days by 365 and then dividing the remainder by the number of days in the week 7 .
Input-:days = 209 Output-: years = 0 weeks = 29 days = 6 Input-: days = 1000 Output-: years = 2 weeks = 38 days = 4
Start Step 1-> declare macro for number of days as const int n=7 Step 2-> Declare function to convert number of days in terms of Years, Weeks and Days void find(int total_days) declare variables as int year, weeks, days Set year = total_days / 365 Set weeks = (total_days % 365) / n Set days = (total_days % 365) % n Print year, weeks and days Step 3-> in main() Declare int Total_days = 209 Call find(Total_days) Stop
Live demonstration
#include <stdio.h> const int n=7 ; //find year, week, days void find(int total_days) { int year, weeks, days; // assuming its not a leap year year = total_days / 365; weeks = (total_days % 365) / n; days = (total_days % 365) % n; printf("years = %d",year); printf("</p><p>weeks = %d", weeks); printf("</p><p>days = %d ",days); } int main() { int Total_days = 209; find(Total_days); return 0; }
If we run the above code , it will generate the following output
years = 0 weeks = 29 days = 6
The above is the detailed content of Write a C program to convert a given number of days into years, weeks and days. For more information, please follow other related articles on the PHP Chinese website!