Java Program to Check Leap Year

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

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:

  1. Using if-else statements for Finding Leap Year in Java
  2. Using the Ternary Operator for Finding Leap Year
  3. Using command-line arguments to check for Leap Year in Java
  4. Using Scanner class to check for Leap Year in Java
  5. 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:

  1. Let the year be y.
  2. 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.
  3. 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.
  4. 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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Code

Output

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.

Leap Year Program in Java

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.

  1. Here, we take the user input directly from the command line when the Java compiled code is executed.
  2. 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.
  3. 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:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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:

  1. We import the Scanner class from the java.util package.
  2. In the main method, we create a Scanner object (named s).
  3. Then we scan an integer using s.nextInt() method and store it in an integer variable.
  4. 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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more