Home >Java >javaTutorial >Leap Year Program in Java
Before starting any program in any programming language, it is essential to understand its logic. 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, a Leap year is the year with 1 extra day in the calendar, i.e., a leap year has 366 days instead of 365, which are in an ordinary year. (February 29 is added in a leap year with 28 days in an ordinary year). From a mathematical perspective, we consider years divisible by 4 as leap years, except for century years. This occurrence happens every 4 years.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Logic:
The main part before writing any program is understanding its logic. Let us know the logic of leap year in a stepwise manner.
Using the 4 steps mentioned above, the leap year program can be easily created in any programming language with the basic if and else statement usage.
To program the leap year in Java, one should know the following:
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 or 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.
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 see some snapshots of the outputs when you execute the above program with different year values. These outputs will help you check whether the user-provided year is a leap year or not:
In the above code, we have implemented the logic mentioned above with 3 steps using the if and else statements. Suppose we dry-run the above code with an input value of 2020. Checking the given year step by step according to the code written.
The programmer can also perform the above task by creating a separate function of the leap year outside the primary function and calling that function from the Java main function, keeping the logic the same. It depends on the choice of programmer and 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.
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 essential to keep the logic of century year in the code; else, the output would be wrong in many cases. Other programs must have the logic before writing the code, as it becomes easy to code once the logic is done.
The above is the detailed content of Leap Year Program in Java. For more information, please follow other related articles on the PHP Chinese website!