Java Program to Check Leap Year
Leap years are those years with 366 days instead of 365. A leap year must satisfy any one of the following two constraints:
- It should be divisible by 400
- It should be divisible by 4 and not by 100
Methods to Check Leap Year in Java
Java provides certain methods to evaluate leap year which are mentioned below:
- Using if-else statements for Finding Leap Year in Java
- Using the Ternary Operator for Finding Leap Year
- Using command-line arguments to check for Leap Year in Java
- Using Scanner class to check for Leap Year in Java
- Using in-built isLeap() method to check for Leap Year in Java
Using if-else statements for Finding Leap Year in Java
The procedure is as follows:
- Let the year be y.
- We start our if-else block and check if the year is divisible by 400, or not, if true, the year is a leap year and we print the same, otherwise we move to the else if block.
- If the first else if block, we check if the year is divisible by 100, if true, the year is not a leap year and we print the result, else we move to the next else if block.
- In the last else if block, we check if the year is divisible by 4 and if that turns out to be true the year is a leap year and we display the result, if that condition fails too, we conclude that the year is not a leap year in our final else block.
Code
Output
Complexity
Time Complexity- O(1)
Space Complexity- O(1)
Using the Ternary Operator for Finding Leap Year
The basic idea of a ternary operator in Java is to reduce the if-else statements, as those can get cumbersome, as we saw in the earlier method.
A general structure for the ternary operator is shown in the image below.
A program to find whether a year is a leap or not using ternary operators in Java will need three nested ternary operators, one for each of the conditions mentioned in our flowchart.
Code:
Output:
Complexity
Time Complexity- O(1)
Space Complexity- O(1)
Using command-line arguments to check for Leap Year in Java
Command-line arguments are stored as strings in the String array passed to the main method in Java classes. We can use this feature to take input directly while running our program after compilation.
- Here, we take the user input directly from the command line when the Java compiled code is executed.
- We read arguments from String[] args in the main method using array syntax and convert them into integers by using the Integer class method Integer.parseInt() which converts other data types to integers.
- We place the input in the checkLeapYear(int year) method and display whether the given year is a leap year or not.
Code:
How to run: Store the code in a file named LeapYear.java, compile it using:
Execute the class file generated using the following command:
Output:
Complexity
Time Complexity- O(1)
Space Complexity- O(1)
Command-line arguments open a completely new domain for taking inputs in java programs and can be helpful at times.
Using Scanner class to check for Leap Year in Java
We can access the Scanner class to take inputs from the command line in Java. The process is as follows:
- We import the Scanner class from the java.util package.
- In the main method, we create a Scanner object (named s).
- Then we scan an integer using s.nextInt() method and store it in an integer variable.
- We supply the value to the checkLeapYear(int year) method and display whether the given year is a leap year or not.
Code:
Output:
Complexity
Time Complexity- O(1)
Space Complexity- O(1)
Using in-built isLeap() method to check for Leap Year in Java
Java provides an in-built method isLeap() in the Year class to identify if an input year is leap or not. Let us see its implementation:
Declaration:
Code:
Output:
Complexity
Time Complexity- O(1)
Space Complexity- O(1)
Conclusion
- Leap years are those years which are either divisible by 400 or they are divisible by 4 and not by 100.
- We can find leap years using simple if-else blocks.
- We can use ternary operators to present the if-else logic in an elegant manner.
- To make the code modular, the whole if-else logic can be shifted to a method which decides and prints whether the input year is leap or not.
- Apart from implementing it from scratch, Java also has a Year class with inbuilt methods like isLeap() to ease some of our work.