In this java program we are going to check whether the input year is a leap year or not. Before we see the program, lets see how to determine whether a year is a leap year mathematically:
To determine whether a year is a leap year, follow these steps:
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int year;
Scanner scan = new Scanner(System.in);
System.out.println("Enter any Year:");
year = scan.nextInt();
scan.close();
boolean isLeap = false;
if(year % 4 == 0)
{
if( year % 100 == 0)
{
if ( year % 400 == 0)
isLeap = true;
else
isLeap = false;
}
else
isLeap = true;
}
else {
isLeap = false;
}
if(isLeap==true)
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}
|
Output:
Enter any Year:
2003
2003 is not a Leap Year.
|