Leap Year Program in Java: A year is a Leap Year if it satisfies the following conditions:
- The year is exactly divisible by 400 (such as 2000,2400) or,
- The year is exactly divisible by 4 (such as 2008, 2012, 2016) and not a multiple of 100 (such as 1900, 2100, 2200).
Write a Java Program to check whether a given year is a leap year or not.
1. Take a year as input.
2. Check whether a given year is divisible by 400.
3. Check whether a given year is divisible by 100.
4. Check whether a given year is divisible by 4.
5. If the condition at step 2 and 4 becomes true, then the year is a leap year.
6. If the condition at step 3 becomes true, then the year is not a leap year.
Here is the source code of the Java Program to Find if a Given Year is a Leap Year. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner; public class Check_Leap_Year { public static void main(String args[]) { Scanner s = new Scanner(System.in); System.out.print("Enter any year:"); int year = s.nextInt(); boolean flag = false; if(year % 400 == 0) { flag = true; } else if (year % 100 == 0) { flag = false; } else if(year % 4 == 0) { flag = true; } else { flag = false; } if(flag) { System.out.println("Year "+year+" is a Leap Year"); } else { System.out.println("Year "+year+" is not a Leap Year"); } } }
1. Enter any year as an input. We first whether the given year is divisible by 400 or not.
2. If it is divisible then it is a leap year else we check for further conditions.
3. Now if it is divisible by 100 then it is not a leap year or else we further divide it by 4.
4. If it is divisible then it is a leap year else its not.
Testcase 1: In this case, we enter the year “1800” as input to check whether a given year is leap year or not.
Enter any year: 1800 1800 is not a Leap Year
Testcase 2: In this case, we enter the year “2000” as input to check whether a given year is leap year or not.
Enter any year: 2000 2000 is a Leap Year
To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.
- Practice Information Technology MCQs
- Check Java Books
- Check Programming Books
- Practice Programming MCQs
- Apply for Java Internship