Java Program to Check Leap Year
A leap year is a calendar year in which an additional day is added to February month. In a leap year, the number of days in February month and the year are 29 and 366 respectively. A year that is not a leap year is called a common year. A year is said to be a leap year if:
- it is divisible by 4.
- it is divisible by 4 but not divisible by 100.
- it is divisible by 4, 100 and 400.
Method 1: Using conditional statements
In the example below, conditional statements are used to identify a leap year.
public class MyClass { public static void main(String[] args) { int year = 2019; if (year % 400 == 0) { System.out.println(year + " is a leap year."); } else if (year % 100 == 0) { System.out.println(year + " is not a leap year."); } else if (year % 4 == 0) { System.out.println(year + " is a leap year."); } else { System.out.println(year + " is not a leap year."); } } }
The above code will give the following output:
2019 is not a leap year.
Method 2: Using Method
In the example below, a method called leapyear() is created which takes year as argument and prints whether the passed year is a leap year or not.
public class MyClass { static void leapyear(int year) { if (year % 400 == 0) { System.out.println(year + " is a leap year."); } else if (year % 100 == 0) { System.out.println(year + " is not a leap 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) { leapyear(2019); } }
The above code will give the following output:
2019 is not a leap year.
Recommended Pages
- Java - Swap two numbers
- Java Program - Fibonacci Sequence
- Java Program - Insertion Sort
- Java Program - Find Factorial of a Number
- Java Program - Find HCF of Two Numbers
- Java Program - Merge Sort
- Java Program - Shell Sort
- Stack in Java
- Queue in Java
- Java Program - Find LCM of Two Numbers
- Java Program - To Check Whether a Number is Palindrome or Not
- Java Program - To Check Whether a String is Palindrome or Not
- Java Program - Heap Sort
- Java Program - Quick Sort
- Java - Swap Two Numbers without using Temporary Variable
- Java Program - To Check Armstrong Number
- Java Program - Counting Sort
- Java Program - Radix Sort
- Java Program - Find Largest Number among Three Numbers
- Java Program - Print Floyd's Triangle