A leap year is a year that is divisible by 4, except for years that are divisible by 100 but not by 400. In Python, you can write a program to check if a given year is a leap year using a simple algorithm. The program will take a year as input and then determine whether it’s a leap year or not.
Concept of Leap Year
The concept of leap years is a way to adjust our calendar system to account for the fact that the Earth’s orbit around the sun takes approximately 365.2422 days.
To keep our calendar year synchronized with the solar year, we add an extra day, known as a leap day, to the calendar periodically. Leap years are those years that have an extra day added to them.
The rules for determining leap years are as follows:
- Divisibility by 4: A year must be divisible by 4 to be a candidate for a leap year.
- Exception for Divisibility by 100: If a year is divisible by 100, it is not a leap year, unless it also satisfies the next rule.
- Exception for Divisibility by 400: If a year is divisible by 400, it is a leap year.
Example –
Year 2020: This year is divisible by 4 (2020 % 4 == 0), so it passes the first rule. It is not divisible by 100 (2020 % 100 != 0), so it doesn’t fail the second rule. Therefore, 2020 is a leap year.
Year 1900: As mentioned earlier, this year is divisible by 4 (1900 % 4 == 0) but is also divisible by 100 (1900 % 100 == 0). However, it fails the third rule because it is not divisible by 400 (1900 % 400 != 0). Therefore, 1900 is not a leap year.
Year 2000: This year is divisible by 4 (2000 % 4 == 0) and is divisible by 100 (2000 % 100 == 0), but it also passes the third rule because it is divisible by 400 (2000 % 400 == 0). Therefore, 2000 is a leap year
Python Program to check leap year
Algorithm
- Start
- Read the input year from the user.
- Check if the year is divisible by 4.
- If it is divisible by 4, check if it is not divisible by 100 or it is divisible by 400.
- If the above conditions are met, then it’s a leap year; otherwise, it’s not.
- Display the result.
- End
Source Code
# Step 1: Read the input year from the user year = int(input("Enter a year: ")) # Step 2: Check if the year is divisible by 4 if (year % 4 == 0): # Step 3: Check if it is not divisible by 100 or it is divisible by 400 if (year % 100 != 0) or (year % 400 == 0): # Step 4: It's a leap year print(year, "is a leap year.") else: # Step 4: It's not a leap year print(year, "is not a leap year.") else: # Step 4: It's not a leap year print(year, "is not a leap year.")
Steps to write code
Step-1: Read Input Year from the User
year = int(input("Enter a year: "))
Step-2: Check if the Year is Divisible by 4
if (year % 4 == 0):
Step-3: Check if it is not Divisible by 100 or it is Divisible by 400
if (year % 100 != 0) or (year % 400 == 0):
Step-4: If the conditions in step 3 are met, print that the year is a leap year
print(year, "is a leap year.")
Step-5: If the conditions in step 3 are not met, print that the year is not a leap year
print(year, "is not a leap year.")
Output
Enter a year: 2020 2020 is a leap year. Enter a year: 1900 1900 is not a leap year. Enter a year: 2000 2000 is a leap year.