In this post, we will learn How to check leap year using Java Programming language. Let’s understand Leap Years and “How a Leap year is Predicted” and then we will be able to code the Java Program to Check Leap Year.
What is Leap Year ?
As we all know in 1 year there is a total of 365 days. A Leap Year, on the other hand, has a total of 366 days i.e. the year has an extra day which is the 29th of February.
Let’s see “How to know if a year is a leap year or not ?”
- Leap Years are years which are exactly divisible by 4.
- But, not exactly divisible by 100.
- But, can be exactly divisible by 400
By using this logic we can now code the Java Program to Check Leap Year.
Java Program to Check Leap Year
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the Year : "); int year = sc.nextInt(); boolean isLeap = false; if(year%4 == 0){ if(year%100 == 0){ if(year % 400 == 0){ isLeap = true; }else{ isLeap = false; } }else{ isLeap = true; } }else{ isLeap = false; } if(isLeap == true){ System.out.println(""+year+" is a Leap Year"); }else{ System.out.println(""+year+" is Not a Leap Year"); } } }
Output 1
Enter the Year: 2028
2028 is a leap year
Output 2
Enter the Year: 2050
2050 is not a leap year
How Does This Program Work ?
Scanner sc = new Scanner(System.in); System.out.println("Enter the Year : "); int year = sc.nextInt();
In this program, we first take the year as an input to check whether it is a leap year or not. We store the input in the year variable of int datatype.
boolean isLeap = false; if(year%4 == 0){ if(year%100 == 0){ if(year % 400 == 0){ isLeap = true; }else{ isLeap = false; } }else{ isLeap = true; } }else{ isLeap = false; }
Then, we declared isLeap
variable of boolean datatype to store true
if the given year is a leap year and false
if it is not a leap year. We are then checking whether the year is divisible by both 4 & 400 and not divisible by 100.
if(isLeap == true){ System.out.println(""+year+" is a Leap Year"); }else{ System.out.println(""+year+" is Not a Leap Year"); }
Then, if the isLeap
is true we display that the given year is a Leap Year and if it is false then we print the given year is not a leap year.
This is the Java Program to Check Leap Year.
Java Program to Check Leap Year
Method 2 : in one line
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); System.out.println("Enter the Year : "); int year = sc.nextInt(); boolean isLeap = false; if( ((year%4 == 0)&&(year%10!=0))||(year%400 == 0)){ isLeap = true; }else{ isLeap = false; } if(isLeap == true){ System.out.println(""+year+" is a Leap Year"); }else{ System.out.println(""+year+" is Not a Leap Year"); } } }
Output 1
Enter the Year : 2400
2400 is a Leap Year
Output 2
Enter the year: 2012
2012 is a leap year
Conclusion
I hope after going through this post, you understand how to code Java Program to Check Leap Year.
If you have any doubt regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.
Also Read: