➤ Check Even Number
➤ Check Odd Number
➤ Java Even-Odd
➤ Greatest of 3 numbers
➤ Exponents in Java
➤ Java Leap Year Program
➤ Display Multiplication Table
➤ Reverse of a number
➤ Factors of a Number
➤ Java LCM of 2 Numbers
➤ Java HCF of 2 Numbers
➤ Quadratic Equation Program
➤ Square Root of Number
➤ Perfect Square Program
➤ Simple Calculator Program
➤ BMI Calculator Java
➤ Factorial of a Number
➤ Factorial Using Recursion
# Java Programs to Find Sum
# Java Conversion Programs
# Java Program on Series
# Java Pattern Programs
# Java Number Programs
Java Array Programs
Java String Programs
In this post, we will write a Leap year Program in Java. Based on the given input it will check the given year is a leap year or not?
A year is called leap year if the year is divisible by four, except for the years which are divisible by 100 but not divisible by 400. Therefore, the year 2000 was a leap year, but the years 1700, 1800, and 1900 were not.
The complete list of leap years in the first half of the 21st century is 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.
Leap year program in Java using if-else
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
// declare variables
int year = 0;
boolean result = false;
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter year:: ");
year = scan.nextInt();
// check year is leap year or not
if(( year%4==0) && ( (year%400==0) || (year%100!=0) ) )
result = true;
else
result = false;
// display result
if(result == true)
System.out.println(year+" is a leap year");
else System.out.println(year+" is not a leap year");
// close Scanner class object
scan.close();
}
}
Output for different test-cases:-
Enter year:: 2000
2000 is a leap year
Enter year:: 1900
1900 is not a leap year
In this program for your better understanding purpose, we have written logic to check year is a leap year or not inside the main method, but we should not do that. We should write logic inside a user-defined method and call it from the main method.
Using the user-defined method
import java.util.Scanner;
public class LeapYear {
public static boolean isLeapYear(int year) {
if(( year%4==0) && ( (year%400==0) || (year%100!=0) ) )
return true;
else
return false;
}
public static void main(String[] args) {
// declare variables
int year = 0;
boolean result = false;
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter year:: ");
year = scan.nextInt();
// check year is leap year or not
result = isLeapYear(year);
// display result
if(result) // true, if result=true
System.out.println(year+" is a leap year");
else System.out.println(year+" is not a leap year");
// close Scanner class object
scan.close();
}
}
In this program, all conditions are written within the if condition, so it may look likes complicated for you. In the below program, the nested-if statement is used to solve the same problem. Now, you may understand the logic simply. The isLeapYear() method of this program also can be written using nested if-else.
Leap year program in Java using nested if-else
import java.util.Scanner;
public class LeapYear {
public static boolean isLeapYear(int year) {
if(year % 4 == 0) {
if(year % 100 == 0) {
if(year % 400 == 0) return true;
else return false;
} else return true;
} else return false;
}
public static void main(String[] args) {
// declare variables
int year = 0;
boolean result = false;
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter year:: ");
year = scan.nextInt();
// check year is leap year or not
result = isLeapYear(year);
// display result
if(result) // true, if result=true
System.out.println(year+" is a leap year");
else System.out.println(year+" is not a leap year");
// close Scanner class object
scan.close();
}
}
The logic of the isLeapYear() method also can be written in one line as,
The isLeapYear() also can be written as,
public static boolean isLeapYear(int year) {
return ((year%4==0)&&((year%400==0)||(year%100!=0)));
}
Java program using the above method,
import java.util.Scanner;
public class LeapYear {
public static boolean isLeapYear(int year) {
return ((year%4==0)&&((year%400==0)||(year%100!=0)));
}
public static void main(String[] args) {
// declare variables
int year = 0;
boolean result = false;
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter year:: ");
year = scan.nextInt();
// check year is leap year or not
result = isLeapYear(year);
// display result
if(result) // true, if result=true
System.out.println(year+" is a leap year");
else System.out.println(year+" is not a leap year");
// close Scanner class object
scan.close();
}
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!