Python Program to Check Leap Year
A leap year is a special year that has an extra day added to it12. This extra day is called leap day and it occurs on February 2923. Leap years have 366 days instead of the usual 365 days
Python Code :
The below Python program checks if a given year is a leap year or not:
# Get input year from user
year = int(input("Enter a year: "))
# Check if the year is a leap year
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
In this program, the user is prompted to enter a year. The program then checks whether the year is a leap year using a series of if and else statements.
A leap year is a year that is divisible by 4, except for years that are divisible by 100 but not divisible by 400.
Therefore, the program first checks whether:
The year is divisible by 4 using the expression year % 4 == 0.
If it is, the program checks whether the year is divisible by 100 using the expression year % 100 == 0.
If it is, the program checks whether the year is divisible by 400 using the expression year % 400 == 0.
If it is, the year is a leap year. Otherwise, it is not a leap year. If the year is not divisible by 100, it is a leap year.
Finally, the program prints out the result using the print function.
Note that this program assumes that the user enters a valid integer. If the user enters an invalid input, such as a string or a non-numeric value, the program will raise a ValueError or produce an incorrect result.
For example :
Enter a year: 1996
1996 is a leap year