Introduction to Leap Year Program in Java
Before starting any program in any programming language, it is important to understand the logic behind it. Once the logic is done in mind and the basic knowledge of programming concepts is known to the programmer, there is no big deal in writing a program. Talking in layman’s language, Leap year is the year having 1 extra day in the calendar, i.e. a leap year has 366 days instead of 365, which are there in an ordinary year. (February 29 is added in a leap year which has 28 days in an ordinary year). Mathematically, years divisible by 4 are considered leap years except for the century years as it occurs after every 4 years.
Logic:
The main part before writing any program is to understand the logic behind it. Let us understand the logic of leap year in a stepwise manner.
- In general, as the leap year occurs after every 4 years, so a leap year is the one that should be evenly divisible by 4.
- Since after every 100 years, we skip a leap year unless it is divisible by 400. So, for a year to be a leap year, it should be divisible by 100.
- If the year is divisible by 100, it should also be divisible by 400; then, it is considered a leap year.
- If the year is divisible by 100 but not by 400, it is not considered to be a leap year.
Using the above mentioned 4 steps, the leap year program can be easily created in any programming language with the basic if and else statement usage.
👉How to check leap year in Java using various methods?
To program the leap year in Java, one should have the knowledge of the following:
- How to read input from the user in Java programming language using various input-output classes like Scanner, Buffered Reader, Input Stream Reader, etc.
- How to use the if and else statements in Java.
Otherwise, logic will remain the same as mentioned above; below given is the detailed algorithm implementing checking whether the given year is a leap year or not:
Step 1: If the given year is evenly divisible by 4, go to step 2; else, go to step 5.
Step 2: If the given year is evenly divisible by 100, go to step 3; else, go to step 4.
Step 3: If the given year is evenly divisible by 400, go to step 4; else, go to step 5.
Step 4: Respective year entered by the user is a leap year.
Step 5: Respective year entered by the user is not a leap year.
Example
We have written the program of leap year in Java, taking the input from the user using the Scanner class.
Code:
import java.util.Scanner;
public class LeapYear {
//main method of java class from where the execution starts
public static void main(String[] args) {
int yr;
// We have used the Scanner class to take the input from the user
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the year you want to test ");
yr = sc.nextInt();
sc.close();
boolean isLeapYear = false;
//Checking the first and foremost condition of leap year
if(yr % 4 == 0)
{
//Checking the second condition of the century year (as we skip a leap year after every 100 years)
if( yr % 100 == 0)
{
//Checking the third condition of the year divisible by 100 and 400 both
if ( yr % 400 == 0)
isLeapYear = true;
else
isLeapYear = false;
}
else
isLeapYear = true;
}
else {
isLeapYear = false;
}
//Final checking the value of boolean variable ‘isLeapYear’ and displaying the final results on the console
if(isLeapYear == true)
System.out.println("Given Year is a Leap Year");
else
System.out.println("Given year is not a Leap Year");
}
}
Please find some of the snapshots of the outputs on executing the above program providing the different values of the years to check whether the year provided by the user is a leap year or not:
In the above code, we have implemented the above-mentioned logic with 3 steps using the if and else statements. Suppose we dry run the above code with an input value 2020. Checking the given year step by step according to the code written.
- Checking the even divisibility of 2020 by 4. Since 2020 %4 ==0, we will move to the second step of the if statement.
- Checking the even divisibility of 2020 by 100. Since 2020 %100 != 0, we will move to the else part. So the value of the boolean variable ‘isLeapYear’ becomes true.
- In the end, checking the value of the variable ‘isLeapYear’(which is a boolean variable holding true or false values). Since it is true, so the text mentioned “Given Year is a Leap Year” is displayed on the console.
The programmer can also perform the above task creating a separate function of the leap year outside the main function and calling that function from the Java main function, keeping the logic the same. It depends on the choice of programmer what type of code he/she prefers (writing the core logic inside the main or in a separate function); for the newbies having less knowledge of the Java input classes, the programmer can perform the same task by directly inputting the year in the code itself and the main function or passing its value while calling its function.
Conclusion
The above description clearly explains what a leap year is, its logic and the code to implement the above logic. Many programmers get confused that leap year is the year that comes after every 4 years and forgets the logic of century year. But it is very important to keep the logic of century year in the code; else, the output would be wrong in many cases. It is important for other programs to have the logic before writing the code, as it becomes easy to code once the logic is done.
Recommended Articles
This is a guide to Leap Year Program in Java. Here we discuss the introduction, How to check leap year in Java using various methods, and the Logics created. You can also go through our other related articles to learn more–
1 Comments
This is good article hai bhai please more than sent article ☺️☺️☺️☺️☺️☺️☺️☺️☺️☺️☺️☺️
ReplyDelete