Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Program to convert given number of days in terms of Years, Weeks and Days in C
You are given a 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 (non-leap year).
Syntax
years = total_days / 365 weeks = (total_days % 365) / 7 days = (total_days % 365) % 7
Formula Explanation
- Number of years = (number of days) / 365 − The quotient obtained by dividing the given number of days with 365
- Number of weeks = (number of days % 365) / 7 − The remaining days after extracting years, divided by 7
- Number of days = (number of days % 365) % 7 − The final remainder after extracting years and weeks
Example
The following program converts days into years, weeks, and remaining days −
#include <stdio.h>
const int DAYS_IN_WEEK = 7;
const int DAYS_IN_YEAR = 365;
// Function to convert total days into years, weeks, and days
void convertDays(int total_days) {
int years, weeks, days;
// Calculate years (assuming non-leap year)
years = total_days / DAYS_IN_YEAR;
// Calculate weeks from remaining days
weeks = (total_days % DAYS_IN_YEAR) / DAYS_IN_WEEK;
// Calculate remaining days
days = (total_days % DAYS_IN_YEAR) % DAYS_IN_WEEK;
printf("Total days: %d<br>", total_days);
printf("Years: %d<br>", years);
printf("Weeks: %d<br>", weeks);
printf("Days: %d<br>", days);
}
int main() {
int total_days = 209;
printf("Example 1:<br>");
convertDays(total_days);
printf("\nExample 2:<br>");
convertDays(1000);
return 0;
}
Example 1: Total days: 209 Years: 0 Weeks: 29 Days: 6 Example 2: Total days: 1000 Years: 2 Weeks: 38 Days: 4
How It Works
For 209 days:
- Years: 209 ÷ 365 = 0 (quotient)
- Remaining days: 209 % 365 = 209
- Weeks: 209 ÷ 7 = 29 (quotient)
- Final days: 209 % 7 = 6 (remainder)
For 1000 days:
- Years: 1000 ÷ 365 = 2 (quotient)
- Remaining days: 1000 % 365 = 270
- Weeks: 270 ÷ 7 = 38 (quotient)
- Final days: 270 % 7 = 4 (remainder)
Conclusion
This program efficiently converts total days into years, weeks, and remaining days using integer division and modulo operations. The formula works for any positive number of days assuming a standard 365-day year.
Advertisements
