Back

Leap Year Program in Java: Simple Code Examples

05 Feb 2025
4 min read

Introduction

Everybody knows that February sometimes gets 29 days on the calendar. That’s because of the leap year concept. A leap year happens every 4 years to match the calendar with Earth’s orbit around the Sun. Learning a leap year program in Java is important since it teaches you logic building and helps solve real-world problems that involve time and date. In this article, we will learn how to write the leap year program in Java.

What is a Leap Year?

A leap year is a year that has an extra day added to February. It makes the total days in the month 29 days instead of the usual 28. This adjustment happens every 4 years to align the calendar with the orbit of the Earth, which takes 365.25 days. But not every year divisible by 4 is a leap year. Century years, which end in 00, are a special case. A century year is only considered a leap year if it is divisible by 400. 

Rules to Verify a Leap Year

A Java program for leap year should follow these rules:

  • If a year is divisible by 4, it may be a leap year. So, we move to the next check.
  • If the year is divisible by 100, we need one more condition to confirm.
  • If the year is also divisible by 400, then it is a leap year.
  • However, if the year is not divisible by 100 but divisible by 4, it is still a leap year.
  • If none of the above conditions are met, then the year is not a leap year.

A leap year code Java can help you automate this logic and get an understanding of how to solve similar problems in coding.

Steps to Find If Given Year Is Leap Year

The leap year program Java should follow these steps: 

Step 1: Check if the year is divisible by 4. If not, it’s not a leap year.

Step 2: If the year is divisible by 4, check if it’s a century year (ending in 00). If it’s not a century year, it’s a leap year.

Step 3: If the year is a century year, check if it is divisible by 400. If yes, it’s a leap year; if no, it’s not a leap year.

Pseudocode For Leap Year Program in Java 

Here is the pseudocode for Java program to check leap year

This pseudocode for the leap year program Java outlines the steps to check if a given year is a leap year. It checks the divisibility of the given year by 4, 100, and 400 to determine if the year meets the leap year conditions.

Algorithm

1. Start

2. Input a year from the user.

3. Check if the year is divisible by 4:

  • If yes, go to step 4.
  • If no, print "Not a leap year" and go to step 7.

4. Check if the year is divisible by 100:

  • If yes, go to step 5.
  • If no, print "Leap year" and go to step 7.

5. Check if the year is divisible by 400:

  • If yes, print "Leap year".
  • If no, print "Not a leap year".

6. End

PseudoCode

Start
    Input year
    If year is divisible by 4
        If year is divisible by 100
            If year is divisible by 400
                Print "Leap year"
            Else
                Print "Not a leap year"
        Else
            Print "Leap year"
    Else
        Print "Not a leap year"
End

Output

Input Year: 2024
Output: 2024 is a leap year

Java Program For Leap Year

In this section, we’ll take a look at the different ways in which to write a leap year program in Java

1. Java Program For Leap Year Using if-else Statements 

Pseudocode

First, let’s look at the leap year in Java code using if-else statements: 1.Start2. Initialize the variable year with the value 2024.3. Check if year is divisible by 4:

  • If true, check if year is divisible by 100:
  • If true, check if year is divisible by 400:
  • If true, print year is a Leap Year.
  • Else, print year is Not a Leap Year.
  • Else, print year is a Leap Year.
  • Else, print year is Not a Leap Year.

4. End

Code

public class Main {
    public static void main(String[] args) {
        int year = 2024; // Year is hardcoded (no Scanner, no command line input)

        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    System.out.println(year + " is a Leap Year");
                } else {
                    System.out.println(year + " is Not a Leap Year");
                }
            } else {
                System.out.println(year + " is a Leap Year");
            }
        } else {
            System.out.println(year + " is Not a Leap Year");
        }
    }
}

How The Code Works

  • The year is set as 2024 in the program.
  • The program checks if the year is divisible by 4 using the modulo operator (%). If year % 4 == 0, it moves to the next check.
  • If divisible by 4, the program checks if the year is divisible by 100 using year % 100 == 0.
  • For years divisible by 100, the program further checks if it's divisible by 400 using year % 400 == 0.
  • Based on these conditions, the program uses nested if-else statements to determine if the year is a leap year.
  • If the year passes all checks, it prints that the year is a Leap Year, otherwise, it prints that it is Not a Leap Year.

Output

2024 is a Leap Year

=== Code Execution Successful ===

Complexity

Time Complexity: O(1) Space Complexity: O(1)

2. Leap Year Program in Java Using Ternary Operator 

Here’s the leap year program in Java, which uses the ternary operator:

Pseudocode

1. Start

2. Create a Scanner object for user input.

3. Prompt the user to enter a year.

4. Read the input and store it in the variable year.

5. Use the ternary operator to determine if the year is a leap year:

  • If year is divisible by 4:
  • Check if year is divisible by 100:
  • If true, check if year is divisible by 400:
  • If true, set the result to "Leap Year".
  • Else, set the result to "Not a Leap Year".
  • Else, set the result to "Leap Year".
  • Else, set the result to "Not a Leap Year".

6. Print the result with the input year.

7. End

Code

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input the year
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();

        // Check if the year is a leap year using the ternary operator
        String result = (year % 4 == 0) ? 
                        ((year % 100 == 0) ? 
                        ((year % 400 == 0) ? "Leap Year" : "Not a Leap Year") 
                        : "Leap Year") 
                        : "Not a Leap Year";

        // Output the result
        System.out.println(year + " is " + result);
    }
}

How The Code Works

The ternary operator is used to replace the if-else statements:

Ternary operator: 

  • The first condition checks if the year is divisible by 4.
  • If true, it checks if the year is divisible by 100.
  • If divisible by 100, it further checks if the year is divisible by 400.
  • The program determines it's not a leap year if any condition fails.

Output 

Enter a year: 2004
2004 is Leap Year

=== Code Execution Successful ===

Complexity: 

Time Complexity: O(1), Space Complexity: O(1)

3. Java Program For Leap Year: Bonus Boolean Method 

Here’s the Java program for checking leap year in Java using a Boolean method:

Pseudocode

1. Start

2. Create a Scanner object for user input.

3. Prompt the user to enter a year.

4. Read the input and store it in the variable year.

5. Call the function isLeapYear(year) to check if the year is a leap year.

6. Inside the isLeapYear(year) function:

  • If year is divisible by 4:
  • Check if year is divisible by 100:
  • If true, check if year is divisible by 400:
  • If true, return true (Leap Year).
  • Else, return false (Not a Leap Year).
  • Else, return true (Leap Year).
  • Else, return false (Not a Leap Year).

7. If isLeapYear(year) returns true, print "year is a Leap Year".8. Else, print "year is Not a Leap Year".

9. End

Code

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Input the year
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();

        // Call the isLeapYear method
        if (isLeapYear(year)) {
            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 isLeapYear(int year) {
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                return year % 400 == 0;
            }
            return true;
        }
        return false;
    }
}

How the Code Works

The program defines a separate Boolean method isLeapYear(), to check if a year is a leap year.

  • It checks the same conditions: divisible by 4, 100, and 400. It returns true for a leap year and false otherwise.
  • The main() method prompts the user to input a year and calls the isLeapYear() method.
  • The result is printed based on the Boolean value returned by the method.

Output

Enter a year: 2010
2010 is Not a Leap Year

=== Code Execution Successful ===

Complexity: 

Time Complexity: O(1), Space Complexity: O(1)

4. Java Program For Leap Year Using Scanner Class 

Here’s the leap year program Java using the Scanner class:

Pseudocode 

1. Start

2. Create a Scanner object for user input.

3. Prompt the user to enter a year.

4. Read the input and store it in the variable year.

5. Check if year is divisible by 4:

  • If true, check if year is divisible by 100:
  • If true, check if year is divisible by 400:
  • If true, print "year is a Leap Year".
  • Else, print "year is Not a Leap Year".
  • Else, print "year is a Leap Year".
  • Else, print "year is Not a Leap Year".

6. End

Code

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Create a Scanner object for input
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter a year
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();

        // Check if the year is a leap year
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    System.out.println(year + " is a Leap Year");
                } else {
                    System.out.println(year + " is Not a Leap Year");
                }
            } else {
                System.out.println(year + " is a Leap Year");
            }
        } else {
            System.out.println(year + " is Not a Leap Year");
        }
    }
}

How the Code Works

1. First we will import the Scanner class from java.util package.The Scanner class is used to get input from the user.

  • Scanner scanner = new Scanner(System.in); initialises the scanner.
  • scanner.nextInt(); reads an integer input (the year).

2. The program evaluates the conditions to check if the year is a leap year:

  • Divisible by 4.
  • If divisible by 100, also divisible by 400.

3. It prints whether the year is a leap year or not.

Output

Enter a year: 2025
2025 is Not a Leap Year

=== Code Execution Successful ===

Complexity 

Time Complexity: O(1), Space Complexity: O(1)

5. Java Program For Leap Year Without Using Scanner Class 

Here’s the Java program to check leap year without using the Scanner class:

Pseudocode

1. Start

2. Initialize the variable year with the value 2024.

3. Check if year is divisible by 4:

  • If true, check if year is divisible by 100:
  • If true, check if year is divisible by 400:
  • If true, print "year is a Leap Year".
  • Else, print "year is Not a Leap Year".
  • Else, print "year is a Leap Year".
  • Else, print "year is Not a Leap Year".

4. End

Code

public class Main {
    public static void main(String[] args) {
        // Year is provided directly in the program
        int year = 2024;

        // Check if the year is a leap year
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    System.out.println(year + " is a Leap Year");
                } else {
                    System.out.println(year + " is Not a Leap Year");
                }
            } else {
                System.out.println(year + " is a Leap Year");
            }
        } else {
            System.out.println(year + " is Not a Leap Year");
        }
    }
}

How the Code Works:

1. The year is hardcoded into the program instead of being input dynamically.

  • For example, in this program, year = 2024.

2. The program uses the same conditional checks as before:

  • Divisible by 4.
  • If divisible by 100, it must also be divisible by 400.

3. Since it is divisible, it prints that 2024 is a leap year.

Output

2024 is a Leap Year

=== Code Execution Successful ===

Complexity 

Time Complexity: O(1), Space Complexity: O(1)

6. Java Program For Leap Year Using In-built isLeap() Method

In this one, we’ll look at the code for the Java program for leap year using in-built isLeap() method: 

Pseudocode 

1. Start

2. Create a Scanner object to take user input.

3. Prompt the user to enter a year.

4. Read the input year and store it in the variable year.

5. Use the Year.of(year).isLeap() method to check if the year is a leap year:

  • If true, print "year is a Leap Year".
  • Else, print "year is Not a Leap Year".

6. End

Code

import java.time.Year;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // Create a Scanner object for user input
        Scanner scanner = new Scanner(System.in);

        // Prompt the user to enter a year
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();

        // Check if the year is a leap year using isLeap() method
        if (Year.of(year).isLeap()) {
            System.out.println(year + " is a Leap Year");
        } else {
            System.out.println(year + " is Not a Leap Year");
        }
    }
}

How the Code Works

1. The program uses the Scanner class to take user input for the year.

2. The Year.of(year).isLeap() method determines if the entered year is a leap year.

3. It prints the result based on the method's return value (true or false).

Output

Enter a year: 2028
2028 is a Leap Year

=== Code Execution Successful ===

Complexity 

Time Complexity: O(1), Space Complexity: O(1)

7. Using the Command Line to Check for Leap Year in Java

Let’s look at how to use the command line to check leap year:

Pseudocode 

1. Start

2. Check if exactly one command-line argument is provided:

  • If not, display an error message and exit.

3. Convert the command-line argument from a string to an integer and store it in year.

4. Check if year is a leap year using the following conditions:

  • If year is divisible by 4, continue checking:
  • If year is also divisible by 100, continue checking:
  • If year is divisible by 400, print "year is a Leap Year".
  • Otherwise, print "year is Not a Leap Year".
  • Otherwise, print "year is a Leap Year".
  • Otherwise, print "year is Not a Leap Year".

5. End

Code

public class Main {
    public static void main(String[] args) {
        // Check if the correct number of arguments is provided
        if (args.length != 1) {
        	System.out.println("Please provide exactly one year as a command-line argument.");
        	return;
        }
 
        // Parse the year from the command-line argument
        int year = Integer.parseInt(args[0]);
 
        // Check if the year is a leap year
        if (year % 4 == 0) {
        	if (year % 100 == 0) {
            	if (year % 400 == 0) {
                	System.out.println(year + " is a Leap Year");
            	} else {
                	System.out.println(year + " is Not a Leap Year");
            	}
            } else {
            	System.out.println(year + " is a Leap Year");
        	}
        } else {
        	System.out.println(year + " is Not a Leap Year");
        }
    }
}

How the code works

1.Argument Check: The program first checks if exactly one command-line argument (year) is provided. If not, it prints an error message and stops.

2. Parse the Year: If one argument is provided, it converts that argument (which is a string) to an integer (year) using Integer.parseInt().

3. Leap Year Check:

  • The program then checks if the year is divisible by 4 using the modulo operator %.
  • If the year is divisible by 4, it checks if it's a century year (divisible by 100).
  • If it is divisible by 100, the program checks if the year is divisible by 400. If true, it prints "Leap Year"; otherwise, it is "Not a Leap Year".
  • If the year is not divisible by 100 but by 4, it prints "Leap Year".

4. Final Output: If the year does not meet the criteria for a leap year (not divisible by 4), it prints "Not a Leap Year".

Output

2024 is a Leap Year

Complexity

Time Complexity: O(1) , Space Complexity: O(1)

Finding Leap Years Within a Range 2025 to 2225

Here’s a program to find all the leap years between 2025 to 2225. It uses a for loop to iterate through the years and a helper function to check if a year is a leap year based on the rules of leap year divisibility discussed in the beginning..

Pseudocode 

 1. Start

2. Define two integer variables:

  • startYear = 2025
  • endYear = 2225

3. Print a message indicating that leap years between startYear and endYear will be displayed.

4. Loop through all years from startYear to endYear:

  • For each year, check if it is a leap year using the isLeapYear(year) function:
  • If year is divisible by 4, continue checking:
  • If year is also divisible by 100, continue checking:
  • If year is divisible by 400, return true (leap year).
  • Otherwise, return false (not a leap year).
  • Otherwise, return true (leap year).
  • Otherwise, return false (not a leap year).
  • If the function returns true, print year.

5. End

Code

public class Main {
    public static void main(String[] args) {
    	int startYear = 2025;
    	int endYear = 2225;
 
    	System.out.println("Leap years between " + startYear + " and " + endYear + ":");
    	
    	for (int year = startYear; year <= endYear; year++) {
        	if (isLeapYear(year)) {
            	System.out.print(year + " ");
        	}
    	}
    }
 
    // Method to check if a year is a leap year
    public static boolean isLeapYear(int year) {
    	if (year % 4 == 0) {
        	if (year % 100 == 0) {
            	return year % 400 == 0;
        	}
        	return true;
    	}
        return false;
    }
}

 How the code works

1. The program defines a starting year as 2025 and an ending year as 2225.

2. It prints a message indicating that it will list all leap years within this range.

3. A for loop iterates through each year from 2025 to 2225.

4. For each year, it checks if the year is divisible by 4:

  • If divisible by 4, it then checks if the year is divisible by 100:
  • If divisible by 100, it further checks if it is divisible by 400:
  • If divisible by 400, the year is a leap year and is printed.
  • Otherwise, it is not a leap year and is skipped.
  • If not divisible by 100, the year is a leap year and is printed.
  • If not divisible by 4, the year is not a leap year and is skipped.

5. The program continues this process every year in the range and prints all leap years.

Output

Leap years between 2025 and 2225:
2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072 2076 2080 2084 2088 2092 2096 2104 2108 2112 2116 2120 2124 2128 2132 2136 2140 2144 2148 2152 2156 2160 2164 2168 2172 2176 2180 2184 2188 2192 2196 2204 2208 2212 2216 2220 2224
=== Code Execution Successful ===

Complexity

Time Complexity: O(N), Space Complexity: O(1)

Conclusion

Learning different ways to write the leap year program in Java, like checking if a year is a leap year, helps build your programming basics. Each method teaches you something new, like how to use conditions, operators, or even in-built functions. These small steps are essential to solving real-world problems and becoming a better programmer.

If you’re a student dreaming of a career in software development, the CCBP 4.0 Academy program is a great place to start. It’s made for beginners to learn programming step by step, with real-world practice and expert guidance.

Frequently Asked Questions

1. What is a leap year?

A leap year has 366 days instead of 365. It happens every 4 years to adjust for the extra 0.25 days Earth takes to orbit the Sun.

2. Why is a leap year important?

Leap years keep our calendar aligned with Earth’s orbit and seasons. They make sure dates stay accurate over time.

3. How do you know if a year is a leap year?

A year is a leap year if it’s divisible by 4. But, if it’s a century year (like 1900), it must also be divisible by 400.

4. What is the easiest way to check a leap year in Java?

You can use the isLeap() method from the java.time package. It checks leap years automatically.

5. Why is learning a leap year program useful?

It helps you practice logic, conditions, and problem-solving. These are skills that are super important in programming.

Read More Articles

Chat with us
Chat with us
Talk to career expert