C Program to Convert Number of Days to Week, Months and Years

  • Write a program in C to convert Number of Days to Days, Weeks, Months and Years.

Required Knowledge

We will first take number of days as input from user using scanf function then convert to Days, Weeks, Months and Years using following conversion equations.

  • 1 Year = 365 Days
  • 1 Month = 30 Days
  • 1 Week = 7 Days

C program to convert number of days to days, weeks, months and years

C Program to Convert Number of Days to Week, Months and Years

/** 
 * C program to convert Numer of Days to 
 * Year, Month, Week and Days 
 */ 
   
#include <stdio.h>  
   
int main() {  
    int inputDays, years, months, weeks, days;  
   
    /* 
     * Take number of days as input from user
     */ 
    printf("Enter number of Days\n");  
    scanf("%d", &inputDays);  
   
    /* 
     * 1 Year = 365 Days, 1 Month = 30 Days, 1 Week = 7 Days
     * To keep things simple, We are not considering Leap years 
     * and assuming 1 Month = 30 Days   
     */ 
    years = inputDays/365;
    // Remaining days after year
    inputDays = inputDays - years*365; 
    months = inputDays/30;
    // Remaining days after month
    inputDays = inputDays - months*30; 
    weeks = inputDays/7;
    // Remaining days after week
    inputDays = inputDays - weeks*7; 
    days = inputDays;
     
    printf("Years : %d\n", years);  
    printf("Months : %d\n", months);  
    printf("Weeks : %d\n", weeks);  
    printf("Days : %d", days);  
   
    return 0;  
}

Output

Enter number of Days
400
Years : 1
Months : 1
Weeks : 0
Days : 5