C# Program to Check Leap Year


Advertisements

A leap year is a calendar year in which an additional day is added to February month. In a leap year, the number of days in February month and the year are 29 and 366 respectively. A year that is not a leap year is called a common year. A year is said to be a leap year if

  • it is divisible by 4.
  • it is divisible by 4 but not divisible by 100.
  • it is divisible by 4, 100 and 400.

Example: Using conditional statements

In the below example, conditional statements are used to identify a leap year.

using System;

namespace MyApplication { 
   class MyProgram {
    static void Main(string[] args) {
      int year = 2019;
      if (year % 400 == 0) {
         Console.Write(year + " is a leap year.");
      } else if (year % 100 == 0) {
         Console.Write(year + " is not a leap year.");
      } else if (year % 4 == 0) {
         Console.Write(year + " is a leap year.");
      } else {
         Console.Write(year + " is not a leap year.");
      }
    }
  }
}

Output

2019 is not a leap year.

Example: Using function

In the below example, a function called leapyear() is created which takes year as argument and prints whether the passed year is a leap year or not.

using System;

namespace MyApplication { 
   class MyProgram {
    static void leapyear(int year) 
    {
      if (year % 400 == 0) {
         Console.Write(year + " is a leap year.");
      } else if (year % 100 == 0) {
         Console.Write(year + " is not a leap year.");
      } else if (year % 4 == 0) {
         Console.Write(year + " is a leap year.");
      } else {
         Console.Write(year + " is not a leap year.");
      }
    }

    static void Main(string[] args) {
     leapyear(2019); 
    }
  }
}

Output

2019 is not a leap year.




Advertisements