You may have been assigned to create a leap year program in java, in your school or college assignment/project, and you are beginner who is searching for help or you may want to create a java program to find leap year and test your own skills, if yes, you have landed on the right place, I will provide you the code to create leap year program in java using if-else or using only if or using OOP and will also explain the working.
Table of Contents
What is leap year?
Usually there are 365 days in a year. However once every four years, there are 366 days in year. This year is called leap year and on a leap year, February has 29 days. However not all years divisible by 4 are leap years.
Now, we know what is leap year, so you must be thinking how should we create a program for this criteria. For issues or to learn programming, you always need to break your large programming issues into smaller chunks first, here are the possible small chunks to make the correct program for leap year:
- A leap year is evenly divisible by 4 except the century years (years like 1200, 1300, 2400, 2500 etc)
- For century years:
- If the century year is divisible by 400 then that year is a Leap year
- If the century year is not divisible by 400 then that year is not a Leap year
Possible Algortihm
if year is divisible by 400 then
is_leap_year
else if year is divisible by 100 then
not_leap_year
else if year is divisible by 4 then
is_leap_year
else
not_leap_year
That's it, now we know what we need to test, in order to know if the year is leap year or not.
Leap year program in Java using If-Else statement
Let's create our java program for leap year using if-else
import java.util.Scanner;
public class Lear_year {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.print("Enter any year:");
int year = s.nextInt();
boolean flag = false;
if (year % 400 == 0) {
flag = true;
} else if (year % 100 == 0) {
flag = false;
} else if (year % 4 == 0) {
flag = true;
} else {
flag = false;
}
if (flag) {
System.out.println("Year " + year + " is a Leap Year");
} else {
System.out.println("Year " + year + " is not a Leap Year");
}
}
}
In the above program, we have imported java.util.Scanner package to get user input, here are basic process which we are following in the above program:
- Get year value from user using Scanner class
- Create a boolean flag, to compare the results in the end, if it is true, then year is a leap year otherwise year is not a leap year.
- Now we have implemented condition using if-else
- If year is divisble by 400 it is a leap year, change flag=true
- if 1 fails, Check if year is divisble by 100, if it is divisble by 100, it is not a leap year, change flag=false
- if both the above conditions are false, check if is divisble by 4, if yes, year is leap year, change flag=true
- If all of the above criteria is false, year is not a leap year, change flag=false
- If year is divisble by 400 it is a leap year, change flag=true
- At the end, check if flag condition is true or false, if true, it is a leap year.
I am executing the above program on a online java compiler (which is not available now) & here is the output
Enter any year:2015
Year 2015 is not a Leap Year
Enter any year:2016
Year 2016 is a Leap Year
Leap year program using If statement
In the above program, we were using if-else statement, now we will reduce the code lines and will be using a single if statement, to check all the conditions using logical operators of java (|| and &&) in single if statement.
So here is our reduced java program
import java.util.Scanner;
public class Lear_year {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
System.out.print("Enter any year:");
int year = s.nextInt();
if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0))) {
System.out.format("\n %d is a Leap Year. \n", year);
}
else {
System.out.format("\n %d is NOT a Leap Year. \n", year);
}
}
}
as you can see, coding line if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0)))
, does all the work which we doing using nested if-else in first program, if you don't have much idea of logical operators, let me explain it quickly.
For || (logical OR), if we have two conditions, conidition1 and condition2, so to execute the next line of code, any (condition1 or conidtion2) of the conditions needs to be true
For && (Logical AND), , if we have two conditions, conidition1 and condition2, so to execute the next line of code, both (condition1 or conidtion2) of the conditions needs to be true
So, considering above line of code, (if year is divisble by 400 OR year is divible by 4) AND (year is not divisble 100), it is leap year.
Here is my output when i tested this on online compiler
Enter any year:2020
2020 is a Leap Year.
Leap year program using Class Object (OOPS)
In this approach we will be using same logic, but will be creating a separate class and then will be calling it from class, by using it's object.
Let's create our main function class, class it LeapYearFunctionClass.java
, so java code for it will be
public class LeapYearFunctionClass {
public boolean CheckLeapYear(int year) {
if (( year%400 == 0)|| (( year%4 == 0 ) && ( year%100 != 0))) {
return true;
}
else {
return false;
}
}
}
In the above funcion CheckLeapYear
we are returning boolean
results, conditions are same as explained above in second approach. Now, in our main function, we need to create above class's object and call it's method CheckLeapYear by passing the year as int.
public class Lear_year {
public static void main(String args[]) {
LeapYearFunctionClass obj = new LeapYearFunctionClass();
System.out.println("2012 is a leap year : " + obj.CheckLeapYear(2012));
System.out.println("2013 is a leap year : " + obj.CheckLeapYear(2013));
System.out.println("2016 is a leap year : " + obj.CheckLeapYear(2016));
}
}
Executing the above code on online complier provide me output as below
2012 is a leap year : true
2013 is a leap year : false
2016 is a leap year : true
There are sevaral other ways of creating leap year program in java, I have discussed these 3 approaches, I hope I have explained things clearly, feel free to ask your questions related to this using below comment's section of in question/forum section.
You may also like to read:
Fibonacci series program in Java (With and without recursion)
Palindrome program in java (String & number example)
Java program to reverse a string (Different ways explained)
Difference between HashMap and Hashtable in Java
Hello World program in Java (CMD and Eclipse Examples)
Servlet in Java with example (Saving form data in database using servlet)