C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Operators & Expressions

Java Program: Find Leap Year with Combined Operators

Leap Year Rules

A year is a leap year if:

  • It is divisible by 400, OR
  • It is divisible by 4 AND not divisible by 100

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();

 

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

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

 

        System.out.println("\n------ Leap Year Result ------");

 

        if (isLeapYear) {

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

        } else {

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

        }

 

        sc.close();

    }

}

Output

 
OUTPUT 1:
INPUT : 
Enter a year: 2024

OUTPUT :
------ Leap Year Result ------
2024 is a Leap Year.

OUTPUT 2:
INPUT : 
Enter a year: 1900

OUTPUT :
------ Leap Year Result ------
1900 is NOT a Leap Year.

OUTPUT 3:
INPUT : 
Enter a year: 2000

OUTPUT :
------ Leap Year Result ------
2000 is a Leap Year.

Explanation

1. Input Year

int year = sc.nextInt();

  • Reads the year from the user.

2. Combined Logical Expression

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

Breakdown:

  • year % 400 == 0 → Leap year (century rule)
  • year % 4 == 0 → Divisible by 4
  • year % 100 != 0 → Not a century year
  • && → AND operator
  • || → OR operator

3. Boolean Variable

boolean isLeapYear = ...

  • Stores the result of the logical expression.

4. Output Decision

if (isLeapYear)

  • Displays result based on boolean value.

✅ Logical Operators Used

Operator

Meaning

&&

Logical AND

`

 

!=

Not equal

%

Modulus