C# Program to check whether entered year is Leap year or not


What is the Leap Year?

Leap year is the year occurring once every four years, having 366 days including 29 February. Below is the program which will determine whether the year entered by user is Leap Year of Not.

using System;
namespace Leap_Year
{
     class Program
     {
      static void Main(string[] args)
      {
         int year;
         Console.WriteLine("Please enter the Year to check for Leap Year in four digits");
         year = Convert.ToInt32(Console.ReadLine());
        if ((year % 4 == 0 && 100 != 0) || (year % 100 == 0))
       {
           Console.WriteLine("Entered Year {0} is the Leap Year", year);
       }
       else
       {
           Console.WriteLine("Entered Year {0} is not the Leap Year", year);
       }
       Console.ReadLine();
      }
     }
}



Output
Please enter the Year to check for Leap Year in four digits
2018
Entered Year 2018 is not the Leap Year


Please enter the Year to check for Leap Year in four digits
2000
Entered Year 2000 is not the Leap Year

Explanation

  • In above program , we prompted user to enter the year the user want to check if it is leap year or not by using Console.WriteLine Statement.

  • The entered value is then accepted using Console.ReadLine() statement and converted into int Datatype because the Console.ReadLine() statement reads the value in String Datatype.

  • Then in the if statement we applied the logic for finding Leap Year and printed the result according to conditions.

  • We can write the same program in a different way. Look at the below program where we have used classes and objects and methods.

    using System;
    namespace Leap_Year
    {
        public class Leap_Calculator_class
        {
         int year;
         public void getUserInput()
         {
          Console.WriteLine("Please enter the Year to check for Leap Year in four digits");
          year = Convert.ToInt32(Console.ReadLine());
         }
         public void calculateLeapYear()
         {
          if ((year % 4 == 0 && 100 != 0) || (year % 100 == 0))
          {
           Console.WriteLine("Entered Year {0} is the Leap Year", year);
          }
          else
          {
           Console.WriteLine("Entered Year {0} is not the Leap Year", year);
          }
          Console.ReadLine();
         }
        }
    }



    using System;
    namespace Leap_Year
    {
    class Program
    {
    static void Main(string[] args)
    {
    Leap_Calculator_class ly = new Leap_Calculator_class();
    ly.getUserInput();
    ly.calculateLeapYear();
    }
    }
    }


    Output
    Please enter the Year to check for Leap Year in four digits
    2018
    Entered Year 2018 is not the Leap Year


    Please enter the Year to check for Leap Year in four digits
    2000
    Entered Year 2000 is not the Leap Year


    Explanation
  • The above program is the modified complex version of the same program to find the entered year by the user is Leap year or not.

  • In this program, we created a public class called Leap_Calculator_class in which we declared and implemented two public methods called getUseInput() and calculateLeapYear().

  • The getUseInput() method will ask user to enter the Year and accept the value and store it in the variable 'year'.

  • In calculateLeapYear() method , we applied the logic to calculate the Leap Year and printed the result on the Console.

  • Then we created another class having main method. We created object of the Leap_Calculator_class and called the two methods.

  • C# Program to check whether entered year is Leap year or not C# Program to check whether entered year is Leap year or not Reviewed by LanguageExpert on February 17, 2018 Rating: 5

    No comments