How to convert days into years months and weeks in C program || Free Code Center

How to convert days into years, months and weeks in C program

Convert day in the year in C

Example 1:

#include <stdio.h>
int main()
{
    double days,year;
    printf("Enter days =");
    scanf("%lf",&days);
    year=days/365;
    printf("Total year is=%.3lf",year);
}

Example 2:

How to convert days into years, months and weeks in C program

#include <stdio.h>

void convertDays(int days, int *years, int *months, int *weeks)
{
    *years = days / 365;
    days %= 365;

    *months = days / 30;
    days %= 30;

    *weeks = days / 7;
    days %= 7;
}

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

    printf("Enter the number of days: ");
    scanf("%d", &days);

    convertDays(days, &years, &months, &weeks);

    printf("Years: %d\n", years);
    printf("Months: %d\n", months);
    printf("Weeks: %d\n", weeks);
    printf("Days: %d\n", days);

    return 0;
}

C programming tutorial

Leave a Comment