Signup/Sign In

Java Program To Check Leap Year

A Leap Year is a year having 366 days. A year is said to be a leap year if the following conditions are satisfied:

  • The year is a multiple of 400.
  • The year is multiple of 4 but not 100.

Here, we are given a year and our task is to check whether the given year is a leap year or not. For example,

Input: 2019

Output: Not a leap year

Program 1: Check Leap Year

In this method, we will directly check whether a year is a leap year or not in the main method itself.

Algorithm

  1. Start
  2. Declare a variable let's say a year.
  3. Initialize it.
  4. Check for the conditions.
  5. If the condition satisfies then it is a leap year else not.
  6. Display the result.
  7. Stop.

Below is the code for the same.

In the below program, we check whether the given year is a leap year or not in the main method itself.

//Java Program to check whether the given year is a leap year or not
import java.util.Scanner;

public class CheckYear
{
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        int year;   //Year Declaration
        System.out.println("Enter the year");
        year=sc.nextInt();   //Year Initialization
       
       //Check for leap year
        if(((year % 4 == 0) && (year % 100 != 0)) ||
            (year % 400 == 0))
        System.out.println(year+" is a leap year");
        else
        System.out.println(year+" is not a leap year");
     } 
}


Enter the year 1998
1998 is not a leap year

Program 2: Check Leap Year

In this method, we will check whether the given year is a leap year or not using conditional operators.

Algorithm:

  1. Start
  2. Declare a variable let's say a year.
  3. Initialize it.
  4. Use a ternary operator to check whether the given year is a leap year or not.
  5. Call a method in the condition section of the ternary operator to check the same.
  6. Return true if the year is a multiple of 400.
  7. Else if the year is a multiple of 100, return false.
  8. Else if the year is a multiple of 4 then it is a leap year and return true. Else return false.
  9. Stop.

Below is the code for the same.

The below example demonstrates how to use a ternary operator to check for a leap year.

//Java Program to check whether the given year is a leap year or not
import java.util.Scanner;

public class CheckYear
{
static boolean checkLeapYear(int year)
    {
        // If a year is multiple of 400, then it is a leap year
        if (year % 400 == 0)
            return true;
     
        // Else If a year is multiple of 100, then it is not a leap year
        if (year % 100 == 0)
            return false;
     
        // Else If a year is multiple of 4, then it is a leap year
        if (year % 4 == 0)
            return true;
        return false;
    }
         
    // Driver method
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        int year;   //Year Declaration
        System.out.println("Enter the year");
        year=sc.nextInt();   //Year Initialization
        
        //Ternary Operator to check
        System.out.println( checkLeapYear(2000)? "Leap Year" :
                           "Not a Leap Year" );    
     }
     
}


Enter the year 2012
Leap Year

Program 3: Check Leap Year

In this method, we will check whether the given year is a leap year or not using functions.

Algorithm:

  1. Start
  2. Declare a variable let's say year.
  3. Initialize it.
  4. Call a function to check.
  5. Return true if the year is a multiple of 400.
  6. Else if the year is a multiple of 100, return false.
  7. Else if the year is a multiple of 4 then it is a leap year and returns true.
  8. Using the if-else condition display the result.
  9. Stop.

Below is the code for the same.

The below example demonstrates how to use functions to check for a leap year.

//Java Program to check whether the given year is a leap year or not using Functions
import java.util.Scanner;

public class CheckYear
{
static boolean checkLeapYear(int year)
    {
        // If a year is multiple of 400, 
        // then it is a leap year
        if (year % 400 == 0)
            return true;
     
        // Else If a year is multiple of 100,
        // then it is not a leap year
        if (year % 100 == 0)
            return false;
     
        // Else If a year is multiple of 4,
        // then it is a leap year
        if (year % 4 == 0)
            return true;
        return false;
    }
         
    // Driver method
     public static void main(String []args)
     {
        Scanner sc=new Scanner(System.in);
        int year;   //Year Declaration
        System.out.println("Enter the year");
        year=sc.nextInt();   //Year Initialization
         boolean check=checkLeapYear(year);
         if(check)
         {
             System.out.println(year+" is a leap year");
         }
         else
         {
            System.out.println(year+" is not a leap year");
         }
     }
     
}


Enter the year 2018
2018 is not a leap year



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.