Create a program that will compute and display the number of days based on the
input starting and ending year.
Sample output:
Enter the starting year: 1999
Enter the ending year: 2001
No. of year/s: 3
No. of days: 1096
Thanks for answering this!
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the starting year: ");
var startingYear = int.Parse(Console.ReadLine());
Console.Write("Enter the ending year: ");
var endingYear = int.Parse(Console.ReadLine());
Console.WriteLine("No. of year/s: {0}", CountYears(startingYear, endingYear));
Console.WriteLine("No. of days: {0}", CountDays(startingYear, endingYear));
}
static int CountYears(int startingYear, int endingYear) => endingYear - startingYear + 1;
static long CountDays(int startingYear, int endingYear)
{
var daysCount = 0L;
for (var currentYear = startingYear; currentYear <= endingYear; currentYear++)
daysCount += DateTime.IsLeapYear(currentYear) ? 366 : 365;
return daysCount;
}
}
}
Comments
Leave a comment