Java Program to Check Leap Year

Introduction

A leap year is a year that is divisible by 4 but not divisible by 100, except if it is also divisible by 400. This ensures that the calendar year stays in sync with the astronomical year. Leap years have 366 days instead of the usual 365, with an extra day in February. This guide will walk you through writing a Java program that checks if a given year is a leap year.

Problem Statement

Create a Java program that:

  • Prompts the user to enter a year.
  • Checks if the year is a leap year.
  • Displays the result.

Example:

  • Input: 2020

  • Output: "2020 is a leap year."

  • Input: 1900

  • Output: "1900 is not a leap year."

Solution Steps

  1. Read the Year: Use the Scanner class to take the year as input from the user.
  2. Check if the Year is a Leap Year: Implement logic to check the conditions for a leap year.
  3. Display the Result: Print whether the year is a leap year or not.

Java Program

// Java Program to Check Leap Year // Author: https://www.rameshfadatare.com/ import java.util.Scanner; public class LeapYearChecker { public static void main(String[] args) { // Step 1: Read the year from the user try (Scanner scanner = new Scanner(System.in)) { System.out.print("Enter a year: "); int year = scanner.nextInt(); // Step 2: Check if the year is a leap year boolean isLeapYear = checkLeapYear(year); // Step 3: Display the result if (isLeapYear) { System.out.println(year + " is a leap year."); } else { System.out.println(year + " is not a leap year."); } } } // Method to check if a year is a leap year public static boolean checkLeapYear(int year) { if (year % 4 == 0) { if (year % 100 == 0) { // year is divisible by 100, so check if it's divisible by 400 return year % 400 == 0; } else { // year is divisible by 4 but not by 100 return true; } } else { // year is not divisible by 4 return false; } } } 

Explanation

Step 1: Read the Year

  • The Scanner class is used to read an integer input from the user, which represents the year. The nextInt() method captures the input year.

Step 2: Check if the Year is a Leap Year

  • The checkLeapYear() method implements the logic to determine if the year is a leap year:
    • A year is a leap year if:
      1. It is divisible by 4.
      2. If it is divisible by 100, it must also be divisible by 400.
    • If the year satisfies these conditions, it is a leap year; otherwise, it is not.

Step 3: Display the Result

  • The program prints whether the year is a leap year or not using System.out.println().

Output Example

Example 1:

Enter a year: 2020 2020 is a leap year. 

Example 2:

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

Example 3:

Enter a year: 2000 2000 is a leap year. 

Conclusion

This Java program demonstrates how to determine whether a given year is a leap year. It covers essential concepts such as conditional statements and user input handling, making it a valuable exercise for beginners learning Java programming.

Leave a Comment

Scroll to Top