In Java, to check whether a year is a leap year or not we can use conditional statements such as if else or switch statements. For a given input year, we have to print whether the year is leap year or not. In this program we will be using the switch statement to solve this problem.
For example, 2000 is a leap year. So, for the input 2000 our output should be given year is leap year. In this article, we will learn through examples, detailed logic and program explanation for better understanding.
Required knowledge
- Java programming basics
- Java operators
- Conditional statements
Problem Statement
We have to write a java program to check whether a year is leap year or not. The program should take a integer number as a year and should print whether the year is leap year or not leap year. This action will be done in our program.
Let’s see an example,
Example 1:
For a given input year 2001, Output should be “2001 is not a leap year”.
Example 2:
For a given input year 2000, Output should be “2000 is a leap year”.
Logic to check a year is leap year or not using switch()
For a given integer input year, initialize result to 0. We should check whether the year is:
- Divisible by 4
- Not divisible by 100 unless it is also divisible by 400
For non-century years (years not ending with 00), if the year is divisible by 4 but not by 100, it is a leap year. For century years (years ending with 00), the year must be divisible by both 100 and 400 to be considered a leap year.
Here’s the breakdown:
- If the year is divisible by 400, it is definitely a leap year.
- If the year is divisible by 100 but not by 400, it is not a leap year.
- Else, if the year is divisible by 4 but not by 100, it is a leap year.
- If the year is not divisible by 4, it is not a leap year.
Using switch, we check the result value with case values. If result is 0, it prints “Leap year.” If result is 1, it prints “Not a leap year.”
Java program to check a year is leap year or not using switch()
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the year");
int year = s.nextInt();
int result;
if((year%4==0 && year%100 !=0)||(year%400==0))
result=0;
else
result=1;
switch(result) {
case 0:
System.out.println(year+" is a leap year");
break;
case 1:
System.out.println(year+" is not a leap year");
}
s.close();
}
}
Output:
Enter the year
2001
2001 is not a leap year
Java program explanation to check a year is leap year or not using switch()
- The above java program imports the Scanner class from the java.util package to enable user input.
- The main method is the entry point of the program. It initializes a Scanner object to read user input.
- The program asks the user to enter a year and reads the input using the nextInt() method of the Scanner class.
- The program applies the leap year logic to determine if the entered year is a leap year or not. The logic is:
- If the year is divisible by 4 and not divisible by 100, or if it’s divisible by 400, it’s a leap year.
- If the conditions are met, result variable assigned with 0; otherwise, it is assigned with 1.
- Program uses a switch statement to check the value of the result variable.
- If the value in result variable is 0, it prints that the entered year is a leap year.
- If the value in result variable is 1, it prints that the entered year is not a leap year.
- Finally, it closes the Scanner object to release system resources and prevent resource leaks.
Conclusion
In this article, we have learnt how to check if a year is leap year or not in Java. We have seen the logic using switch and implemented the program. Hope this article helped you in the understanding of the peogram.
You can also check another awesome tutorials
- Java program to print maximum among two using switch statement
- Java program to print all uppercase alphabets
- Print Fibonacci series in Java
- Java program to print all even numbers in a given range
Happy Coding!!