1. Introduction
In this post, we will create a simple Java program to determine whether a given year is a leap year or not. A leap year is exactly divisible by 4 except for end-of-century years which must be divisible by 400. This means that the year 2000 was a leap year, although 1900 was not.
2. Program Steps
1. Define the main class named LeapYearChecker.
2. Inside the main class, define the main method.
3. Inside the main method, define a variable year and assign the value of the year we want to check.
4. Check if the year is a leap year using conditional statements.
5. Print whether the given year is a leap year or not.
3. Code Program
public class LeapYearChecker { // 1. Defining the main class
public static void main(String[] args) { // 2. Defining the main method
int year = 2020; // 3. Assigning the year we want to check
// 4. Checking if the year is a leap year
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
// 5. Printing the result
System.out.println(year + " is a Leap Year");
} else {
// 5. Printing the result
System.out.println(year + " is not a Leap Year");
}
}
}
Output:
2020 is a Leap Year
4. Step By Step Explanation
– Step 1: We define the main class named LeapYearChecker.
– Step 2: The main method is defined inside the main class. This method will be executed by the JVM.
– Step 3: Inside the main method, a variable year is defined and assigned the value of the year we want to check, which is 2020 in this example.
– Step 4: We then check whether the year is a leap year or not. According to the Gregorian calendar, a year is a leap year if it is exactly divisible by 4 but not by 100. However, years divisible by 400 are also leap years. So, the year 2000 was a leap year, while 1900 was not.
– Step 5: Based on the condition, the program prints whether the given year is a leap year or not. In this case, it prints "2020 is a Leap Year" to the console.