Program to convert given number of days in terms of Years, Weeks and Days in C

CServer Side ProgrammingProgramming

You are given with number of days, and the task is to convert the given number of days in terms of years, weeks and days.

Let us assume the number of days in a year =365

Number of year = (number of days) / 365

Explanation-: number of years will be the quotient obtained by dividing the given number of days with 365

Number of weeks = (number of days % 365) / 7

Explanation-: number of weeks will be obtained by collecting the remainder from dividing the number of days with 365 and further dividing the result with number of days in a week which is 7.

Number of days = (number of days % 365) % 7

Explanation-: number of days will be obtained by collecting the remainder from dividing the number of days with 365 and further acquiring the remainder by dividing the partial remainder with number of days in a week which is 7.

Example

Input-:days = 209
Output-: years = 0
weeks = 29
days = 6
Input-: days = 1000
Output-: years = 2
weeks = 38
days = 4

Algorithm

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

Example

Live Demo

#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("\nweeks = %d", weeks);
printf("\ndays = %d ",days);
}
int main() {
int Total_days = 209;
find(Total_days);
return 0;
}

Output

IF WE RUN THE ABOVE CODE IT WILL GENERATE FOLLOWING OUTPUT

years = 0
weeks = 29
days = 6