Check a given year is a leap year or not in Java.
Java Code: Java Program to Check Leap Year
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public static void main(String[] args) { // input from standard input - keyboard Scanner reader = new Scanner(System.in); System.out.print("Enter a year(yyyy): "); // nextInt() reads the next integer from the keyboard int year = reader.nextInt(); boolean leap = false; if(year % 4 == 0) { if( year % 100 == 0) { // year is divisible by 400, hence the year is a leap year if ( year % 400 == 0) leap = true; else leap = false; } else leap = true; } else leap = false; if(leap) System.out.println(year + " is a leap year."); else System.out.println(year + " is not a leap year."); } |
Output:
In this example you have learned How do you check if it is a leap year?