This program converts a given number of days into years, weeks, and remaining days using integer division and modulo arithmetic. The formula uses 365 days per year and 7 days per week (leap years are ignored). It is a straightforward exercise in chained division and remainder operations.
The original post used void main() and broken \n in all output strings. This rewrite fixes both and adds input validation.
Conversion Formula
| Value | Formula | Example (375 days) |
|---|---|---|
| Years | total / 365 |
375 / 365 = 1 year |
| Remaining days | total % 365 |
375 % 365 = 10 days remaining |
| Weeks | (total % 365) / 7 |
10 / 7 = 1 week |
| Leftover days | (total % 365) % 7 |
10 % 7 = 3 days |
C Program: Days to Years, Weeks, and Days
/* Convert a number of days to years, weeks, and days
* Compile: gcc -ansi -Wall -Wextra days.c -o days */
#include <stdio.h>
#define DAYS_PER_YEAR 365
#define DAYS_PER_WEEK 7
int main(void)
{
int total, years, weeks, days;
printf("Enter number of days: ");
if (scanf("%d", &total) != 1 || total < 0) {
printf("Invalid input.\n");
return 1;
}
years = total / DAYS_PER_YEAR;
weeks = (total % DAYS_PER_YEAR) / DAYS_PER_WEEK;
days = (total % DAYS_PER_YEAR) % DAYS_PER_WEEK;
printf("%d days = %d year(s), %d week(s), %d day(s)\n",
total, years, weeks, days);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra days.c -o days
./days
Sample Output
Enter number of days: 375 375 days = 1 year(s), 1 week(s), 3 day(s) Enter number of days: 365 365 days = 1 year(s), 0 week(s), 0 day(s) Enter number of days: 1 1 days = 0 year(s), 0 week(s), 1 day(s) Enter number of days: 0 0 days = 0 year(s), 0 week(s), 0 day(s)
Step-by-Step Trace (375 days)
years = 375 / 365 = 1375 % 365 = 10(remaining days after extracting years)weeks = 10 / 7 = 110 % 7 = 3(remaining days after extracting weeks)- Output: 1 year(s), 1 week(s), 3 day(s)
Code Explanation
- #define constants for clarity —
DAYS_PER_YEARandDAYS_PER_WEEKmake the formula readable. If the definition of a week or year ever needs changing (e.g., adding leap year support), only the#definelines change — not every place the number appears in the code. - Chained division and modulo — the pattern
value / unitextracts how many complete units fit, andvalue % unitgives the remainder. Applied twice: first to extract years (with remainder in days), then to extract weeks from the remaining days. - Leap years ignored — the program uses exactly 365 days per year. For calendar-accurate conversion including leap years, you would need to count actual calendar years iteratively or use a date library.
- Integer arithmetic for days — days, weeks, and years are all whole numbers, so
intis the right type. No floating-point needed. Integer division in C truncates toward zero, which is the desired behavior here.
What This Program Teaches
- Division and modulo as a pair —
/gives the quotient (how many fit),%gives the remainder (what’s left). This pair is useful whenever you need to break a quantity into larger and smaller units: seconds to hours/minutes/seconds, bytes to kilobytes/bytes, etc. - Use #define for magic numbers — raw numbers like 365 and 7 inside formulas are “magic numbers” — their meaning is not obvious to a reader. Named constants make the intent explicit and prevent the same number from being used inconsistently in different places.
- Validate scanf return value —
if (scanf("%d", &total) != 1)catches non-numeric input. Without this check,totalwould be uninitialized if the user types letters, leading to undefined behavior.
Related Programs
- Simple Interest in C
- sizeof Data Types in C
- Global and Local Variables in C
- Time Functions in C (time.h)
- All Data Types in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.