C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Introduction to Java

Java Program: Leap Year Check Using Logical Operators

Leap Year Condition

A year is a leap year if:

Leap Year Condition

✅ Uses logical operators: &&, ||, !=

import java.util.Scanner;

 

public class LeapYearCheck {

    public static void main(String[] args) {

 

        Scanner sc = new Scanner(System.in);

 

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

        int year = sc.nextInt();

 

        // Leap year condition using logical operators

        boolean isLeap = (year % 400 == 0) ||

                         (year % 4 == 0 && year % 100 != 0);

 

        if (isLeap) {

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

        } else {

            System.out.println(year + " is NOT a Leap Year.");

        }

 

        sc.close();

    }

}

Output

 

EXAMPLE 1:

INPUT:
Enter a year: 2024
 
OUTPUT :
2024 is a Leap Year.
 
EXAMPLE 2:

INPUT:
Enter a year: 1900
 
OUTPUT :
1900 is NOT a Leap Year.
 

Explanation

1. What makes a year a leap year?

A year is a leap year if any of the following is true:

  1. Divisible by 400 → Leap year
  2. Divisible by 4 but not divisible by 100 → Leap year
  3. Else → NOT a leap year

These rules ensure the calendar stays aligned with the earth’s revolution.

2. Logical Operators Used

  • && → AND
  • || → OR
  • != → NOT equal

The condition:

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

Means:

  • If divisible by 400 → leap
  • OR
  • If divisible by 4 AND NOT divisible by 100 → leap

3. Why 1900 is not a leap year?

  • 1900 % 4 == 0 ✅
  • 1900 % 100 == 0 ✅
  • 1900 % 400 != 0 ❌

Fails the 400 rule → NOT a leap year.