Program to Convert a Given Number of Days into Years, Months, Weeks, and Days [C, C++, Python & Java]

Posted in

Program to Convert a Given Number of Days into Years, Months, Weeks, and Days [C, C++, Python & Java]
vinaykhatri

Vinay Khatri
Last updated on December 27, 2024

    This tutorial will help you write a program in C, C++, Python, and Java to convert the number of days into years, months, weeks, and days.

    We know that 1 year = 365 days, 52 weeks, and 12 months. However, if we say 406 days, it becomes 1 year, 1 month, 1 week, and 4 days. Let us see mathematically how to convert the number of days into years, months, weeks, and days.

    • Initially, divide the number of days by 365, i.e., the number of days/365. The quotient will be the number of years. Use the mod operator (%) to get the remaining days.
    • Divide the number of remaining days obtained in Step 1 by 30. The quotient will be the number of days. Again, use the mod operator to get the remaining days.
    • Divide the number of remaining days obtained in Step 2 by 7. The quotient will be the number of days, and the remaining days will be the number of days.

    Now, we know the approach. Let us implement it in a program.

    C Program to Convert Days into Years, Months, Weeks, and Days

    #include<stdio.h>
    int main()
    {
        int days,temp1,temp2,week,mon,year,d;
        printf("Enter the days: ");
        scanf("%d", &days);
        year = days/365;
        temp1= days%365;
        mon = temp1/30;
        temp2 = temp1%30;
        week = temp2/7;
        d = temp2%7;
        printf("The number of years is: %d", year);
        printf("\nThe number of months is: %d", mon);
        printf("\nThe number of weeks is: %d", week);
        printf("\nThe number of days is: %d", d);
        return 0;
    }

    Output:

    Enter the days: 3456
    The number of years is: 9
    The number of months is: 5
    The number of weeks is: 3
    The number of days is: 0

    C++ Program to Convert Days into Years, Months, Weeks, and Days

    #include<iostream>
    using namespace std;
    int main()
    {
        int days,temp1,temp2,week,mon,year,d;
        cout<<"Enter the days: ";
        cin>>days;
        year = days/365;
        temp1= days%365;
        mon = temp1/30;
        temp2 = temp1%30;
        week = temp2/7;
        d = temp2%7;
        cout<<"The number of years is: "<<year;
        cout<<"\nThe number of months is: "<<mon;
        cout<<"\nThe number of weeks is: "<<week;
        cout<<"\nThe number of days is: "<<d;
        return 0;
    }

    Output:

    Enter the days: 4567
    The number of years is: 12
    The number of months is: 6
    The number of weeks is: 1
    The number of days is: 0

    Python Program to Convert Days into Years, Months, Weeks, and Days

    days = int(input("Enter the days: "))
    year = days//365
    temp1 = days%365
    mon = temp1//30
    temp2 = temp1%30
    week = temp2//7
    d = temp2%7
    print("The number of years is:",year)
    print("The number of months is:",mon)
    print("The number of weeks is:",week)
    print("The number of days is:",d)

    Output:

    Enter the days: 3214
    The number of years is: 8
    The number of months is: 9
    The number of weeks is: 3
    The number of days is: 3

    Java Program to Convert Days into Years, Months, Weeks, and Days

    import java.util.*;
    public class Main
    {
        public static void main(String[] args)
        {
            int days,temp1,temp2,week,mon,year,d;
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter the days: ");
            days = sc.nextInt();
            year = days/365;
            temp1= days%365;
            mon = temp1/30;
            temp2 = temp1%30;
            week = temp2/7;
            d = temp2%7;
            System.out.println("The number of years is: " +year);
            System.out.println("The number of months is: " +mon);
            System.out.println("The number of weeks is: " +week);
            System.out.println("The number of days is: " +d);
       }
    }

    Output:

    Enter the days: 
    4567
    The number of years is: 12
    The number of months is: 6
    The number of weeks is: 1
    The number of days is: 0

    Conclusion

    That's it! We have written a program in C, C++, Python, and Java to convert the number of days into years, months, weeks, and days. It is important to note that we wrote the above programs considering 365 days in a year. It is not valid for leap years. For more accurate calculations, you need to consider the leap years and the varying month lengths.

    People are also reading:

    Leave a Comment on this Post

    0 Comments