C# Program to Convert Days into Years, Months and Days
Posted by Superadmin on August 11 2022 08:45:17

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).

 

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