In this program, you'll learn to check wheather the given Year (integer) is a leap year or not.
To understand this example, you should have the knowledge of the following Java programming topics:
A java program that checks wheather the given year is leap year or not is as follows:
import java.util.Scanner;
class checkLeapYear{
public static void main(String[] args){
int year;
boolean isLeapYear;
System.out.print("Enter the year to check: ");
// instance of scanner class
Scanner input = new Scanner(System.in);
// taking user input in year variable
year = input.nextInt();
// checking weather the year is leap or not
isLeapYear = year % 4 == 0;
if(isLeapYear){
System.out.println(year + " is leap year");
}else{
System.out.println(year + " isn't leap year");
}
input.close();
}
}
Enter the year to check: 2003
2003 isn't leap year
Enter the year to check: 2004
2004 is leap year
To check weather a year
is leap
or not we only need to check weather it’s divisible by 4
or not if it doesn’t leave reminder then leap year else not a leap year
Don’t know how to take input from the user ? Look at this examples
Here two input numbers are taken from user one after another with space in between them which distinguish between two different inputs, this useful behavior is because of the default settings of Scanner called as Delimiter, learn more here.