Leap Year Program in Java
Below is the implementation of Leap Year:
Java
// Java program to find a leap year // Importing Classes/Files import java.io.*; // Class for leap-year dealing public class w3wiki { // Method to check leap year public static void isLeapYear( int year) { // flag to take a non-leap year by default boolean is_leap_year = false ; // If year is divisible by 4 if (year % 4 == 0 ) { is_leap_year = true ; // To identify whether it is a // century year or not if (year % 100 == 0 ) { // Checking if year is divisible by 400 // therefore century leap year if (year % 400 == 0 ) is_leap_year = true ; else is_leap_year = false ; } } // We land here when corresponding if fails // If year is not divisible by 4 else // Flag dealing- Non leap-year is_leap_year = false ; if (!is_leap_year) System.out.println(year + " : Non Leap-year" ); else System.out.println(year + " : Leap-year" ); } // Driver Code public static void main(String[] args) { // Calling our function by // passing century year not divisible by 400 isLeapYear( 2000 ); // Calling our function by // passing Non-century year isLeapYear( 2002 ); } } |
2000 : Leap-year 2002 : Non Leap-year
The complexity of the above method
Time Complexity: O(1)
Auxiliary Space: O(1)
Explaination of the above Program:
As we check that 2000 is a century year divisible by 100 and 4. 2002 is not divisible by 4, therefore not a leap year. Using these facts we can check any year if it is leap or not.
Java Program to Find if a Given Year is a Leap Year
Leap Year contains 366 days, which comes once every four years. In this article, we will learn how to write the leap year program in Java.
Facts about Leap Year
Every leap year corresponds to these facts :
- A century year is a year ending with 00. A century year 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.
- A century year should be divisible by 4 and 100 both.
- A non-century year should be divisible only by 4.