w3resource

Java Leap Year Checker Project

Two Solutions for Checking Leap Year in Java:

Leap Year Checker:

Input: A year.
Output: Whether the year is a leap year or not.

Example:

  • Input: 2024
  • Output: "2024 is a leap year."
  • Input: 2023
  • Output: "2023 is not a leap year."

Solution 1: Leap Year Checker using if-else

This solution checks if a year is a leap year using basic if-else conditions.

Code:

import java.util.Scanner;

public class LeapYearCheckerIfElse {

    // Method to check if a year is a leap year
    public static boolean isLeapYear(int year) {
        // A leap year is divisible by 4 but not divisible by 100, except when divisible by 400
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                return year % 400 == 0;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a year to check if it's a leap 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();
    }
}

Output:

Enter a year to check if it's a leap year:
 1900
1900 is not a leap year
Enter a year to check if it's a leap year:
 1020
1020 is a leap year.

Explanation :

  • Leap Year Rule: A year is a leap year if:
    • Divisible by 4
    • Not divisible by 100 unless divisible by 400
  • Input: User inputs a year.
  • isLeapYear(): This method checks the leap year condition using if-else logic.
  • Output: Displays whether the input year is a leap year or not.

Solution 2: Leap Year Checker using Ternary Operator

This solution checks if a year is a leap year using a ternary operator for a concise approach.

Code:

import java.util.Scanner;

public class LeapYearCheckerTernary {

    // Method to check if a year is a leap year using a ternary operator
    public static boolean isLeapYear(int year) {
        // Using ternary operator for leap year check
        return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a year to check if it's a leap year:");
        int year = scanner.nextInt();

        // Using ternary operator for result output
        String result = isLeapYear(year) ? year + " is a leap year." : year + " is not a leap year.";
        System.out.println(result);
        scanner.close();
    }
}  

Output:

 Enter a year to check if it's a leap year:
 2000
2000 is a leap year.
Enter a year to check if it's a leap year:
 1961
1961 is not a leap year.

Explanation:

  • Ternary Operator: Used for both leap year check and result output.
  • Input: User inputs a year.
  • isLeapYear(): Checks the leap year condition in one line using a ternary operator.
  • Output: Displays whether the input year is a leap year or not.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/projects/java/java-project-leap-year-checker.php