TeachingBee

Leap Year Program In Java

Leap year program in java

In this article we will look into how to check if a year is leap year or not and also leap year program in Java. But first let’s look into definition of leap year.

What is a Leap Year?

leap year is a year which has 366 days. The extra day is the 29th February. There is a leap year every four years.

Every leap year has following properties :

  • A century year i.e. year ending with 00 like 1900 is a leap year only if it is divisible by 400.
  • A leap year (except a century year) can be identified if it is exactly divisible by 4.

So therefore, a century year should be divisible by 4 and 100 both whereas  non-century year should be divisible only by 4.

Leap Year Program In Java
Leap Year Program In Java

Now let us see leap year program in java.

Leap Year Program In Java

Algorithm:

  1. First, check if year is divisible by 4 or not i.e. year % 4 == 0
  2. If no, return false as it is not a leap year.
  3. If yes, we need to check if year is century year by checking remainder with 100 i.e year % 100 == 0
  4. If it is a century year, check whether year is divisible by 400 or not.
  5. If century year is divisible by 400, it is leap year else not.
  6. If it is not century year, since we have already checked it is divisible by 4, hence it is leap year

Implementation:

// Leap Year Program In Java
import java.io.*;

// Class for leap-year dealing
public class LeapYear {

    // Method to check leap year
    public static boolean isLeapYear(int year) {
        // Check if year is divisble by 4
        if (year % 4 == 0) {
            // Check if it is century year or not
            if (year % 100 == 0) {
                // Check leap year for century year by dividing by 400
                if (year % 400 == 0)
                    return true;
                else
                    return false;
            }

            // If it is not century year, since it is divisible by 4, it is leap year
            return true;
        }
        // since year is not divisble by 4, it is not leap year.
        return false;

    }

    public static void main(String[] args) {
        int year = 2000;
        if (isLeapYear(year))
            System.out.println(year + " : Leap-year");
        else
            System.out.println(year + " : Not Leap-year");
    }
}

Complexity: O(1)

Alternative Way 1

// Leap Year Program In Java
import java.io.*;

// Class for leap-year dealing
public class LeapYear {

    // Method to check leap year
    public static void isLeapYear(int year) {
        //Checking leap year for Century Year
        if (year % 400 == 0) {
            System.out.println(year + " is a leap year");
        }
        // Check if year was century or not.
        else if (year % 100 == 0) {
            System.out.println(year + " is not a leap year");
        }
        // Checking leap year for non century year
        else if (year % 4 == 0) {
            System.out.println(year + " is a leap year");
        } else {
            System.out.println(year + " is not a leap year");
        }

    }

    public static void main(String[] args) {
        isLeapYear(2000);
        isLeapYear(1900);
    }
}

Alternative Way 2

In this approach we will condense the all the if else conditions above to single statement.

  1. The condensed formula to check leap year will be (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))
  2. The condition checks leap for century year (year % 400 == 0)
  3. And the condition checks for non century year ((year % 4 == 0) && (year % 100 != 0))
  4. If any of the above is true, it is leap year, else not

Implementation

// Leap Year Program In Java
import java.io.*;

// Class for leap-year dealing
public class LeapYear {

    // Method to check leap year
    public static boolean isLeapYear(int year) {
        if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {

            // check for century and non century year
            return true;
        }

        // If above condition not true, it is non leap year
        return false;
    }

    public static void main(String[] args) {
        int year = 2000;
        if (isLeapYear(year))
            System.out.println(year + " : Leap-year");
        else
            System.out.println(year + " : Not Leap-year");
    }
}

Conclusion

In this article we discussed various leap Year program in Java. So to conclude, a year if it is a century year should be divisible by 4 and 100 both whereas  non-century year should be divisible only by 4 to be classified as leap year.

Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

ascii value in java

Print ASCII Value in Java

In this article we will into the ASCII table in C, exploring its various character sets—from control characters to printable characters, including letters, digits, and special symbols—and demonstrates how to

data hiding in java

Data Hiding In Java

In this article we will discuss data hiding in Java which is an important concept in object-oriented programming. We will cover So let’s get started. What is Data Hiding in

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In