Write A Program To Check Leap Year In Java

Chapter: Date and Time Last Updated: 22-06-2023 14:07:27 UTC

Program:

            /* ............... START ............... */
                

import java.util.Scanner;

public class LeapYearChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();

        if (isLeapYear(year)) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
    }

    public static boolean isLeapYear(int year) {
        // Leap year is divisible by 4 but not divisible by 100
        // unless it is divisible by 400
        if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
            return true;
        }
        return false;
    }
}

                /* ............... END ............... */
        

Output

Input: 2020
Output: 2020 is a leap year.

Input: 2021
Output: 2021 is not a leap year.

Input: 1900
Output: 1900 is not a leap year.

Input: 2000
Output: 2000 is a leap year.

Input: 2024
Output: 2024 is a leap year.

Notes:

  • In this program, we use a Scanner to read input from the user. The isLeapYear method takes an integer year as input and returns true if it is a leap year, and false otherwise. The logic for checking leap year is as follows:
  • A year is a leap year if it is divisible by 4.
  • However, if the year is divisible by 100, it is not a leap year, unless it is divisible by 400.
  • The isLeapYear method implements this logic and returns the result.
  • In the main method, we prompt the user to enter a year, and then call the isLeapYear method to check whether it is a leap year or not. The program then prints the appropriate message based on the result.

Tags

Leap year program in Java using Scanner # Write A Program To Check Leap Year In Java # Java Program to Check Leap Year

Similar Programs Chapter Last Updated
Java Program to Calculate Time Difference In Hours Date and Time 20-09-2023
Java Code To Calculate Time Difference In Milliseconds Date and Time 20-09-2023
Java Program To Calculate Time Difference In Seconds Date and Time 20-09-2023
How to calculate time difference between two time zones in Java Date and Time 20-09-2023
Java Date And Time API To Create A Custom Calendar Date and Time 20-09-2023
How To Format Date And Time In Java Date and Time 16-09-2023
How Do You Get The First Day Of The Week In Java Date and Time 15-09-2023
Java Code To Change Date Format Date and Time 15-08-2023
Java Program To Find Day Of The Week For A Given Date Date and Time 10-08-2023
Java code to check if date is weekend Date and Time 29-06-2023
Java program to find number of days between two dates Date and Time 29-06-2023
Java Program To Get The Current Time In Different Time Zones Date and Time 22-06-2023
How To Add Working Days To A Date In Java Date and Time 22-06-2023
Java Program To Add A Specified Number Of Days To A Given Date Date and Time 22-06-2023
Write A Program To Display Current Date And Time In Java Date and Time 22-06-2023
Java Date Format AM PM Date and Time 14-05-2023
Java Add Months To Date Date and Time 14-05-2023
Add Days To Date In Java example Date and Time 14-05-2023
Java Program To Calculate Days Between Two Dates Date and Time 16-04-2023
Java Program To Add Days To Date Date and Time 16-04-2023
How To Find Difference Between Two Dates In Java Date and Time 01-04-2023
Add Days To Date Java Example Date and Time 08-08-2021
Time Difference Between Two Timestamps In Java Date and Time 22-09-2018

1