C Program to Convert Number of Days to Years, Months & Days

This C program reads number of days from user and then converts it to number of years, months and days.

Note: while converting given number of days to years, months and days integer division is used. In C, when an integer is divided by another integer then the result is also an integer.

C Source Code: Days to Years, Months & Days Conversion


#include<stdio.h>

int main()
{
    int number_of_days, years, months, days;

    /* Reading number of days from user */
    printf("Enter number of days: ");
    scanf("%d", &number_of_days);

    /* Calculating years */
    years = number_of_days / 365;

    // Calculating months
    months = (number_of_days - years *365) / 30;

    // Calculating days
    days = (number_of_days - years * 365 - months*30);

    // Displaying results
    printf("Years = %d", years);
    printf("\nMonths = %d", months);
    printf("\nDays = %d", days);

    return 0;
}

Output

Enter number of days: 3000
Years = 8
Months = 2
Days = 20