← Back

29. Convert Days to Years, Weeks, and Days

Medium

📝 Problem

Write a program to calculate the number of years, weeks and the remaining days for the given total days

Input Format:
Accept an integer as an input
Output Format:
Display the computed years, weeks and days as follows:
Number of Years:NO._OF_COMPLETE_YEARS
Number of Week:NO._OF_WEEKS_LEFTOUT
Number of Days:NO._OF_DAYS_LEFTOUT
Constraints:
0 <= input <= 10^5
Sample Input 1:
1234
Sample Output 1:
Number of Years:3
Number of Week:19
Number of Days:6
Sample Input 2:
4987
Sample Output 2:
Number of Years:13
Number of Week:34
Number of Days:4
🔗
Similar Pattern
Uses the same division & modulo technique! Check it out for more examples.
🎯 Unit Decomposition: The Division-Modulo Pattern Strikes Again!

The Challenge: Breaking Down Days

Just like converting seconds to time format (Question #27), this problem demonstrates the universal pattern of hierarchical unit decomposition. You're extracting larger units first, then progressively smaller ones from the remainder!

💡 Core Concept
The Same Powerful Pattern:

1. Extract the largest unit using division (/)
2. Get the remainder using modulo (%)
3. Repeat with the next smaller unit

This pattern works for time, currency, data sizes, and ANY hierarchical units!

1️⃣ Understanding the Time Hierarchy

Days to Years, Weeks, Days Breakdown:

📌 The Conversion Rules:
1 year  = 365 days  // Simplified (ignoring leap years)
1 week  = 7 days

// Example: 1234 days breakdown
// Step 1: How many complete years?
Years = 1234 / 365 = 3 years

// Step 2: Days remaining after years
Remaining = 1234 % 365 = 139 days

// Step 3: Convert remaining to weeks
Weeks = 139 / 7 = 19 weeks

// Step 4: Days left after weeks
Days = 139 % 7 = 6 days

// ✓ Result: 3 years, 19 weeks, 6 days

2️⃣ Step-by-Step Algorithm Walkthrough

📌 Complete Trace of Example 1 (1234 days):
int totalDays = 1234;

// STEP 1: Extract complete years
int years = totalDays / 365;
// years = 1234 / 365 = 3
// (3 complete years = 1095 days)

// STEP 2: Get remaining days after removing years
totalDays = totalDays % 365;
// totalDays = 1234 % 365 = 139 days left

// STEP 3: Extract complete weeks from remainder
int weeks = totalDays / 7;
// weeks = 139 / 7 = 19 weeks
// (19 weeks = 133 days)

// STEP 4: Get final remaining days
int days = totalDays % 7;
// days = 139 % 7 = 6 days

// Verification:
// (3 × 365) + (19 × 7) + 6 = 1095 + 133 + 6 = 1234 ✓

3️⃣ Visual Breakdown: Example 2 (4987 days)

📌 Detailed Calculation for 4987 days:
// Start: 4987 total days

// === EXTRACT YEARS ===
years = 4987 / 365 = 13 years
// 13 years = 13 × 365 = 4745 days used

remaining = 4987 % 365 = 242 days
// 242 days left to process

// === EXTRACT WEEKS ===
weeks = 242 / 7 = 34 weeks
// 34 weeks = 34 × 7 = 238 days used

days = 242 % 7 = 4 days
// 4 days remaining

// === FINAL ANSWER ===
// Years: 13, Weeks: 34, Days: 4

// Verification:
// (13 × 365) + (34 × 7) + 4
// = 4745 + 238 + 4
// = 4987 ✓ Perfect!

4️⃣ The Pattern: Compare with Question #27

Aspect Question #27 (Time) Question #29 (Days)
Input Total seconds Total days
Largest Unit Hours (3600 sec) Years (365 days)
Middle Unit Minutes (60 sec) Weeks (7 days)
Smallest Unit Seconds Days
Pattern Divide → Modulo → Repeat
🎓 Universal Pattern Recognition
Notice how the SAME algorithm works for different units:

• Seconds → Hours, Minutes, Seconds
• Days → Years, Weeks, Days
• Bytes → GB, MB, KB, Bytes
• Cents → Dollars, Quarters, Dimes, Pennies

Master this pattern once, use it everywhere!

5️⃣ Code Breakdown & Output Formatting

📌 Complete Solution Explained:
#include <iostream>
using namespace std;

int main() {
    int days;
    cin >> days;
    
    // Extract years (365 days = 1 year)
    int years = days / 365;
    days = days % 365;  // Update days to remainder
    
    // Extract weeks (7 days = 1 week)
    int weeks = days / 7;
    days = days % 7;    // Final remaining days
    
    // Output in exact format required
    cout << "Number of Years:" << years << endl;
    cout << "Number of Week:" << weeks << endl;
    cout << "Number of Days:" << days;
    
    return 0;
}
⚠️ Key Points About the Code
1. Variable Reuse: We reuse days variable to store remainders
2. Order Matters: Always extract largest units first
3. Format Precision: Notice "Number of Week:" (singular) in output format
4. No endl on last line: Last output doesn't have newline

6️⃣ Real-World Applications

Where This Pattern is Used:

  • Age Calculators: Converting total days since birth to years, months, days
  • Project Management: Breaking down project duration into phases
  • Subscription Services: Converting billing periods
  • Fitness Trackers: Goal tracking (days → weeks → months)
  • Calendar Applications: Date arithmetic and conversions
  • Historical Date Calculations: Time between events
  • Warranty Period Display: "3 years, 2 weeks remaining"

7️⃣ Edge Cases & Considerations

📌 Testing Edge Cases:
// Case 1: Zero days
Input: 0
Output: Years:0, Week:0, Days:0

// Case 2: Less than a week
Input: 5
Output: Years:0, Week:0, Days:5

// Case 3: Exactly one year
Input: 365
Output: Years:1, Week:0, Days:0

// Case 4: One year + some weeks
Input: 400
Output: Years:1, Week:5, Days:0
// 400 - 365 = 35 days, 35/7 = 5 weeks

// Case 5: Maximum constraint
Input: 100000
Output: Years:273, Week:44, Days:5
// 100000 / 365 = 273, remainder = 155
// 155 / 7 = 22, remainder = 1... (calculate it!)

8️⃣ Important Note: Leap Years

📅 Simplification in This Problem
This problem uses 365 days per year for simplicity. In real-world applications:

Regular year: 365 days
Leap year: 366 days (every 4 years, with exceptions)
Average year: 365.25 days (accounting for leap years)

For production code dealing with real dates, use date/time libraries that handle leap years, time zones, and calendar complexities!

9️⃣ The Mathematical Invariant

🎓 Verification Formula:

Total Days = (Years × 365) + (Weeks × 7) + Days

This formula should always reconstruct your original input!

Example 1: (3 × 365) + (19 × 7) + 6 = 1095 + 133 + 6 = 1234 ✓
Example 2: (13 × 365) + (34 × 7) + 4 = 4745 + 238 + 4 = 4987 ✓

🔟 Practice Extension Challenge

📌 Try This: Add Months to the Output!
// Extended version: Days → Years, Months, Weeks, Days
// Assuming 30 days per month (simplified)

int years = days / 365;
days = days % 365;

int months = days / 30;
days = days % 30;

int weeks = days / 7;
days = days % 7;

// Now you have: Years, Months, Weeks, Days!
// Same pattern, just one more level!
🎯 Master Takeaway: The division-modulo pattern for hierarchical unit decomposition is a fundamental algorithm that appears across countless programming problems. Master it once, recognize it everywhere!
💻 Solution Code
#include <iostream>

using namespace std;

int main() {
    int days;
    cin >> days;
    
    int years = days / 365;
    days = days % 365;
    
    int weeks = days / 7;
    days = days % 7;
    
    cout << "Number of Years:" << years << endl;
    cout << "Number of Week:" << weeks << endl;
    cout << "Number of Days:" << days;
    
    return 0;
}