In Java, we can check if a year is a leap year using either an if-else condition or a ternary operator. This article focuses on explaining this concept through examples, an algorithm, detailed logic, and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements
- Ternary Operator
Problem Statement
We need to write a Java program to check if a year is a leap year or not. The program should take a year as input and output whether it is a leap year or not. This above action should be done in our program. Let’s look at an example,
Example 1:
Input: 2000 Output: This year is a leap year.
Here , for the input 2000, Output is “This year is a leap year” as 2000 is divisible by 4, 100 and 400
Example 2:
Input: 2002 Output: This year is not a leap year
Here, for the input 2002, Output is ” This year is not a leap year” as 2002 is divisible by 100 but not divisible by 4 and 400.
Algorithm to Check Leap Year
- Start: Begin the algorithm with input year year
- Leap Year: Comes after every 4 years and has 366 days
- Condition Check: Here, we will check if a year divisible by 4 for normal years but for century years it must divisible by 4 and 400.
- Even if the year divisible by 100, we must check divisibility with 400 and if it is divisible by 400 then it is leap year else not
- year%4==0 && year%100!=0 or year%400==0 , then it is leap year.
- year%4==0, year%100==0 and year%400==0 , in this case also it is leap year as we get the divisibility with 4 and 400 along with 100 , Else it is not leap year.
- End: The algorithm terminates with the result of the condition check.
Logic to Check Leap Year
There are multiple logics to check a given input year is leap year or not in Java Programing language.
Logic 1: Using if-else
- Leap year comes after every 4 years and has 366 days
- Take the input year from user
- Leap year is exactly divisible by 4, expect for century years (years end with 00)
- year%4==0 and year%100!=0
- The century year is leap year, if and only it is perfectly divisible by 400
- year%400==0
- In another way, we can say if a year is divisible by 4,100 and 400, then it is leap year else it is non-leap year
if((year%4==0 && year%100!=0)||(year%400==0)
System.out.println("leap year");
else
System.out.println("not a leap year");
Logic 2: Ternary Operator
- This operator takes three operands and works similar to the if else statements. If condition is true, it prints first expression and if condition becomes false then it prints second expression.
- Leap year is exactly divisible by 4, expect for century years (years end with 00)
- year%4==0 and year%100!=0
- The century year is leap year, if and only it is perfectly divisible by 400
- year%400==0
- In another way, we can say if a year is divisible by 4,100 and 400, then it is leap year else it is non-leap year
result=((year%4==0 && year%100!=0)||(year%400==0)?
year+"is leap year" : year+"not a leap year";
Java Program to Check leap year
Program 1: Using if-else
In the below program we are using if-else statement to check if a year is leap year or not in Java.
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();
if((year%4==0 && year%100 !=0)||(year%400==0))
System.out.println("leap year");
else
System.out.println("not leap year");
s.close();
}
}
Output:
enter the year
2007
not leap year
Program Explanation to check leap year
- The program imports the Scanner class to facilitate user input.
- Defines a class named LeapYear.
- In the main method:
- Creates a Scanner object ‘s’ to read user input.
- Asks the user to enter a year.
- Reads the integer input and stores it in ‘year’.
- Checks if the year is divisible by 4 but not by 100 (or divisible by 400), which indicates a leap year.
- If the condition is met, it prints “leap year”.
- Otherwise, it prints “not leap year”.
- Closes the Scanner object to release system resources.
Program 2: Using if-else-if
In the below program we are using if-else-if to check leap year or not in Java.
import java.util.Scanner;
public class LeapYearOtherWay {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter the year");
int year = s.nextInt();
boolean leap =false;
if(year%4==0)
leap=true;
else if(year %100==0)
leap =false;
else if(year %400==0)
leap =true;
else
leap =false;
System.out.println(year+" : "+" leap year : " + leap);
s.close();
}
}
Output:
enter the year
2002
2002 : leap year : false
Program Explanation to check leap year
- The program imports the Scanner class to facilitate user input.
- Defines a class named LeapYearOtherWay.
- In the main method:
- Creates a Scanner object ‘s’ to read user input.
- Asks the user to enter a year.
- Reads the integer input and stores it in ‘year’.
- Initializes a boolean variable ‘leap’ to false.
- Checks if the year is divisible by 4:
- If it is, sets ‘leap’ to true.
- Otherwise, checks if it’s divisible by 100:
- If it is, sets ‘leap’ to false.
- If not, checks if it’s divisible by 400:
- Else, sets ‘leap’ to true.
- If not, leaves ‘leap’ as false.
- Prints whether the year is a leap year or not.
- Closes the Scanner object to release system resources.
Program 3: Using Ternary Operator
In the below program we are using Ternary Operator to check leap year or not in Java..
import java.util.Scanner;
public class LeapYearTernaryOperator {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter the year");
int year = s.nextInt();
String result;
result=((year%4==0 && year%100 !=0)||(year%400==0))?
year+" leap year":
year+" Not a leap year";
System.out.println(result);
s.close();
}
}
Output:
enter the year
2000
2000 leap year
Program Explanation to check leap year
- The program imports the Scanner class to facilitate user input.
- Defines a class named LeapYearTernaryOperator.
- In the main method:
- Creates a Scanner object ‘s’ to read user input.
- Asks the user to enter a year.
- Reads the integer input and stores it in ‘year’.
- Uses a ternary operator to assign the appropriate message to the variable ‘result’ based on whether the year is a leap year or not.
- Prints the ‘result’ message.
- Closes the Scanner object to release system resources.
Conclusion
In this article, we explored how to check a year if it is leap year or not using conditional statements like if-else and if-else-if statements and the ternary operator. We understand the logic behind this and moved on to writing the program.
For more information on related topics, you can find these articles which might be helpful:
- Understanding the Conditional statements in Java
- Concepts of Operators in Java
- Top 10 Java Programming tips
We hope you find this tutorial helpful. If you face any issue or need any clarification, feel free to leave a comment below. Don’t forget to subscribe our newsletter to get more updates related to Java programming and tips.
Stay Connected to our website and Happy Programming!