Leap Year Checker in Java

This program checks if a given year is a leap year. A year is a leap year if:

  • It is divisible by 4
  • Except for end-of-century years, which must be divisible by 400
  • This means that the year 2000 was a leap year, although 1900 was not

Program Structure and Explanation

The Java program follows these steps:

  1. Define the isLeapYear method which takes an integer year as an argument.
  2. Check if the year is divisible by 4.
  3. If true, check if the year is also divisible by 100.
  4. If true, check if the year is divisible by 400.
  5. If any condition fails, return false; otherwise, return true if all conditions pass.
  6. Test the method with a sample year.

Java Program


/**
 * This class provides a method to check if a given year is a leap year.
 */
public class LeapYearChecker {

    /**
     * Checks if a given year is a leap year.
     * 
     * @param year the year to check
     * @return true if the year is a leap year, false otherwise
     */
    public static boolean isLeapYear(int year) {
        // A leap year is divisible by 4
        if (year % 4 == 0) {
            // If the year is also divisible by 100, it must be divisible by 400 to be a leap year
            if (year % 100 == 0) {
                return year % 400 == 0;
            } else {
                return true; // If not divisible by 100, it is a leap year
            }
        } else {
            return false; // Not divisible by 4, not a leap year
        }
    }

    /**
     * Main method to test the isLeapYear method.
     * 
     * @param args command-line arguments
     */
    public static void main(String[] args) {
        int year = 2024; // Example year to test
        if (isLeapYear(year)) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
    }
}

Explanation of the Code

The isLeapYear method first checks if the year is divisible by 4. If it is, the method then checks if the year is also divisible by 100. If both conditions are true, the method checks if the year is divisible by 400. If the year passes all these checks, it is a leap year; otherwise, it is not. The main method demonstrates how to use the isLeapYear method with an example year.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)