Home  >  Article  >  Java  >  Leap Year Program in Java

Leap Year Program in Java

WBOY
WBOYOriginal
2024-08-30 16:28:07437browse

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.

  1. In general, as the leap year occurs after every 4 years, so a leap year should be evenly divisible by 4.
  2. 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.
  3. If the year is divisible by 100, it should also be divisible by 400; then, it is considered a leap year.
  4. We don’t consider a year a leap year if it’s divisible by 100 but not 400.

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.

How to check leap year in Java using various methods?

To program the leap year in Java, one should know the following:

  • How to read input from the user in Java programming language using various input-output classes like Scanner, BufferedReader, 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 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.

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 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:

Leap Year Program in Java

Leap Year Program in Java

Leap Year Program in Java

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.

  1. Checking the even divisibility of 2020 by 4. Since 2020 %4 ==0, we will move to the second step of the if statement.
  2. 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.
  3. In the end, you are checking the value of the variable ‘isLeapYear’ (which is a boolean variable holding true or false values). Since it is true, the text mentioned “Given Year is a Leap Year” is displayed on the console.

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.

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 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:String Comparison in JavaNext article:String Comparison in Java