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


This Java program checks whether a given year is a leap year or not.

  • The program first prompts the user to input a year using the Scanner class, and then checks whether the year is divisible by 4, 100, and 400.
  • If the year is divisible by 400, it is a leap year. If the year is divisible by 100 but not by 400, it is not a leap year. If the year is divisible by 4 but not by 100, it is a leap year. Otherwise, it is not a leap year.
  • The program uses a boolean variable flag to store whether the year is a leap year or not, and then prints out the result based on the value of flag.

Source Code

import java.util.Scanner;
class Leap_Year
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
                System.out.print("Enter the Year :");
                int year = input.nextInt();
                boolean flag = false;
                if(year % 400 == 0)
                       flag = true;
                else if (year % 100 == 0)
                       flag = false;
                else if(year % 4 == 0)
                       flag = true;
                else
                       flag = false;
 
                 if(flag)
                 {
                        System.out.println("Year "+year+" is a Leap Year");
                 }
                 else
                 {
                        System.out.println("Year "+year+" is not a Leap Year");
                 }
	}
}

Output

Enter the Year :2000
Year 2000 is a Leap Year

Example Programs