w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Display the number of days of the year between two specified years

Secondary Nav

C# Sharp DateTime : Exercise-4 with Solution

Write a C# Sharp program to display the number of days of the year between two specified years.

Expected Output :

12/31/2000: day 366 of 2000 (Leap Year)                                 
12/31/2001: day 365 of 2001                                             
12/31/2002: day 365 of 2002                                             
12/31/2003: day 365 of 2003                                             
12/31/2004: day 366 of 2004 (Leap Year)                                 
12/31/2005: day 365 of 2005                                             
12/31/2006: day 365 of 2006                                             
12/31/2007: day 365 of 2007                                             
12/31/2008: day 366 of 2008 (Leap Year)                                 
12/31/2009: day 365 of 2009                                             
12/31/2010: day 365 of 2010                                             
12/31/2011: day 365 of 2011                                             
12/31/2012: day 366 of 2012 (Leap Year)                                 
12/31/2013: day 365 of 2013                                             
12/31/2014: day 365 of 2014                                             
12/31/2015: day 365 of 2015                                             
12/31/2016: day 366 of 2016 (Leap Year)                                 
12/31/2017: day 365 of 2017                                             
12/31/2018: day 365 of 2018                                             
12/31/2019: day 365 of 2019                                             
12/31/2020: day 366 of 2020 (Leap Year)

C# Sharp Code:

 using System;
public class Example4
{
   public static void Main()
   {
      DateTime dec31 = new DateTime(2000, 12, 31);
      for (int ctr = 0; ctr <= 20; ctr++) {
         DateTime dateToDisplay = dec31.AddYears(ctr);
         Console.WriteLine("{0:d}: day {1} of {2} {3}", dateToDisplay, 
                           dateToDisplay.DayOfYear,
                           dateToDisplay.Year,   
                           DateTime.IsLeapYear(dateToDisplay.Year) ? 
                                               "(Leap Year)" : ""); 
      }  

   }
}  

C# Sharp Practice online:

using System;
public class Example4
{
   public static void Main()
   {
      DateTime dec31 = new DateTime(2000, 12, 31);
      for (int ctr = 0; ctr <= 20; ctr++) {
         DateTime dateToDisplay = dec31.AddYears(ctr);
         Console.WriteLine("{0:d}: day {1} of {2} {3}", dateToDisplay, 
                           dateToDisplay.DayOfYear,
                           dateToDisplay.Year,   
                           DateTime.IsLeapYear(dateToDisplay.Year) ? 
                                               "(Leap Year)" : ""); 
      }  

   }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Python: How to check whether a file exists using Python?

C#: Loops in c#

PHP: For each

SQL: JOIN using more than 5 tables

JavaScript: Blank Slide in Javascript Slideshow