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

This is a C# Program to convert a given number of days in terms of years, weeks & days.

Problem Description

This C# Program Converts a Given Number of Days in terms of Years, Weeks & Days.

Problem Solution

Here this program accepts the number of days. Given the number of days, then it calculates the years, weeks & days for this number.

Program/Source Code

Here is source code of the C# Program to Convert a Given Number of Days in terms of Years, Weeks & Days. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Convert a Given Number of Days in terms of 
 * Years, Weeks & Days
 */
using System;
class program
{
    public static void Main()
    {
        int ndays, year, week, days, DAYSINWEEK=7;
        Console.WriteLine("Enter the number of days");
        ndays = int.Parse(Console.ReadLine());
        year = ndays / 365;
        week = (ndays % 365) / DAYSINWEEK;
        days = (ndays % 365) % DAYSINWEEK;
        Console.WriteLine("{0} is equivalent to {1}years, {2}weeks and {3}days", 
                           ndays, year, week, days);
        Console.ReadLine();
    }
}
Program Explanation

In this C program, we are reading the number of days using ‘ndays’ integer variable. Convert the given number of days to a measure of time given in years, weeks and days. For example 375 days is equal to 1 year, 1 week, and 3 days (ignore leap year).

advertisement
advertisement

The year variable is used to calculate the number of years from the value of ‘ndays’ variable. Divide the value of ‘ndays’ variable by 365. Compute the modulus of the value of ‘ndays’ variable by 365 and divide the resulted value by the value of ‘daysinweek’. Compute the number of days using the modulus of the value of ‘ndays’ variable by 365. Print the number of days in terms of years, weeks and days.

Runtime Test Cases
 
Enter the number of days
1000
1000 is equivalent to 2 years, 38 weeks and 4 days

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.