Java Leap Year Program reads an year from the user and returns the message whether the entered year is a leap Year or not. The program is created by using Nested-If Statement for dividing the year number with 4, 100 and 400 respectively in nested If statement.
Two variables are used in the program. Integer variable IntYear stores the year value entered by the user. The Boolean variable booLeap stores true if the year is a leap year else it stores false.
The value of the booLeap is checked to display the appropriate message after checking.
Java Leap Year Code
Let’s see the implementation of program Java Leap Year.
import java.util.Scanner; public class LeapYear { public static void main(String[] args) { Scanner scObj= new Scanner(System.in); //Read year value from user System.out.println("Enter the year"); int intYear = scObj.nextInt(); //declare and initialise variable boolean booLeap = false; if (intYear % 4 == 0) { if (intYear % 100 == 0) { if (intYear % 400 == 0) booLeap = true; else booLeap = false; } else booLeap = true; } else booLeap = false; //displaying the output whether the entered year value is leap or not if (booLeap) System.out.println(intYear + " is a leap year."); else System.out.println(intYear + " is not a leap year."); } }
Output
Read here to understand all about a leap year.