If you have any query feel free to chat us!
Happy Coding! Happy Learning!
A leap year is a year that contains an extra day, February 29th, making it 366 days long instead of the usual 365 days. Leap years are added to keep the calendar year synchronized with the astronomical year, which is approximately 365.2422 days long.
In the Gregorian calendar, which is the calendar system used in most of the world, a year is a leap year if it meets the following conditions:
Here's a Java program to check if a given year is a leap year or not:
javaCopy code
import java.util.Scanner;
public class LeapYearChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
// Check if the year is a leap year
boolean isLeapYear = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
isLeapYear = true;
}
if (isLeapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
scanner.close();
}
}
Example output:
yamlCopy code
Enter a year: 2024
2024 is a leap year.
In the program above, we take the user input for the year and store it in the variable year
. We then use the if
statement to check the conditions for a leap year. If the year meets either of the conditions, we set the isLeapYear
variable to true
, indicating that it is a leap year. Otherwise, the variable remains false
, indicating that it is not a leap year.
The program then prints the result to the console based on the value of isLeapYear
.
Comments: 0