Check Leap Year in Java

check leap year in java: The Java program is designed to determine whether a given year is a leap year or not. A leap year is a year that has an extra day, February 29th, and follows a specific set of rules: it must be divisible by 4, except for years that are divisible by 100 but not divisible by 400.

Java Program to check Leap Year

In Java, you can check if a year is a leap year using a simple algorithm.

This algorithm first checks if the year is divisible by 4. If it is, it proceeds to check if it’s divisible by 100. If it is divisible by 100, it then checks if it’s also divisible by 400. If all these conditions are met, the year is considered a leap year; otherwise, it’s not.

Algorithm

  1. Start
  2. Input the year you want to check.
  3. If the year is divisible by 4, go to step 4. Otherwise, go to step 8.
  4. If the year is divisible by 100, go to step 5. Otherwise, go to step 6.
  5. If the year is divisible by 400, go to step 6. Otherwise, go to step 8.
  6. The year is a leap year. Output “Leap year.”
  7. Stop
  8. The year is not a leap year. Output “Not a leap year.”
  9. Stop

Source Code

In this program, the isLeapYear method takes an integer year as input and returns true if it’s a leap year and false otherwise. The logic follows the rules mentioned earlier: divisible by 4, not divisible by 100 (unless divisible by 400).

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.");
        }

        scanner.close();
    }

    public static boolean isLeapYear(int year) {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            return true;
        } else {
            return false;
        }
    }
}

Steps to Write Program

Step-1: Import the Scanner class from the java.util package to enable user input handling.

import java.util.Scanner;

Step-2: Define a Java class named LeapYearChecker to encapsulate the functionality of checking whether a year is a leap year or not.

public class LeapYearChecker {

Step-3: Create the main method, which serves as the entry point of the program.

public static void main(String[] args) {

Step-4: Create a Scanner object named scanner to read user input from the console.

Scanner scanner = new Scanner(System.in);

Step-5: Display the prompt “Enter a year: ” to instruct the user to input a year.

System.out.print("Enter a year: ");

Step-6: Read an integer input from the user and store it in the year variable using the nextInt() method of the scanner object.

int year = scanner.nextInt();

Step-7: Check whether the input year is a leap year using the isLeapYear method, which will be explained in the next step.

Step-8: If the isLeapYear method returns true, print a message indicating that the input year is a leap year.

System.out.println(year + " is a leap year.");

Step-9: If the isLeapYear method returns false, print a message indicating that the input year is not a leap year.

System.out.println(year + " is not a leap year.");

Step-10: Close the scanner to release system resources once the user input is no longer needed.

    scanner.close();
}

Step-11: Define the isLeapYear method with the following signature, which checks whether a given year is a leap year:

public static boolean isLeapYear(int year) {

Step-12: Check if the year is divisible by 4 but not divisible by 100. or checks if the year is divisible by 400

if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0 {

Step-13: If either of the conditions in step 12 is met, the method returns true, indicating that the input year is indeed a leap year.

return true;

Step-14: If neither of the conditions in steps 12 and 14 is met, the method proceeds to the else block, where it returns false.

    } else {
        return false;
    }
}

Output

The output of the Java program provided in the original code depends on the input you provide when running the program.

The program prompts the user to enter a year, and then it checks whether that year is a leap year or not using the isLeapYear method.

If you enter a year that is a leap year, the program will print a message indicating that it is a leap year.

Enter a year: 2020
2020 is a leap year.

If you enter a year that is not a leap year, the program will print a message indicating that it is not a leap year

Enter a year: 2021
2021 is not a leap year.