Author : Rahul

Write a program to check whether entered year is a leap year or not


Leap Year

A year is said to be Leap year if it is completely divisible by 400 or 4 but not completely divisible by 100. For Example 1992 is a Leap year because it is completely divisible by 4 but 1900 is not a Leap year because it is not divisible by 400 or 4 but it is divisible by 100.

Program

using System;

namespace ProgrammingQuestion
{
 class Program
 {
   static void Main(string[] args)
   {
     //Variable Declaration
     string yearVal;
     int year;
     bool isLeapYear = false;
     
     //User Input
     Console.Write("Enter Year:");
     yearVal = Console.ReadLine();
     year = Convert.ToInt32(yearVal);

     //Leap Year Logic
     if ((year % 400 == 0 || year % 4 == 0) && (year % 100 != 0))
     {
       isLeapYear = true;
     }
     
     //Print Result
     if (isLeapYear)
     {
       Console.WriteLine($"Year {year} is a Leap Year.");
     }
     else
     {
       Console.WriteLine($"Year {year} is not a Leap Year.");
     }      
     Console.Read();
   }
 }
}

Output

Enter Year:2020
Year 2020 is a Leap Year.
Enter Year:1900
Year 1900 is not a Leap Year.