In this article, we are going learn how to check if the given year is a leap year or not using Java Programming and we will use Java if-else condition to solve this problem.
Leap Year are those Year in which we have 366 days whereas a normal Year contains 365 days. In leap year, the month Feburary contain 29 days. (alert-success)
Algorithm for Leap Year.
Below algorithm used to check if a year is a leap year or not:
- Take an input year from the user.
- If the year is evenly divisible by 4, go to step 3. Otherwise, go to step 6.
- If the year is evenly divisible by 100, go to step 4. Otherwise, go to step 5.
- If the year is evenly divisible by 400, go to step 5. Otherwise, go to step 6.
- The year is a leap year (it has 366 days).
- The year is not a leap year (it has 365 days).
Java Code Implementation to Find Leap Year.
import java.util.Scanner; public class LeapYear { public static void main(String[] args) { Scanner input = new Scanner(System.in); int year; // Prompt user to enter a year System.out.print("Enter a year: "); year = input.nextInt(); // Check if the entered year is a leap year if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { System.out.println(year + " is a leap year"); } else { System.out.println(year + " is not a leap year"); } } }
Enter a year: 2020
2020 is a leap year
Enter a year: 2022
2020 is not a leap year



Trends is an amazing magazine Blogger theme that is easy to customize and change to fit your needs.