In this Tutorial -:

Understanding the Problem
A leap year is a year that contains an extra day, February 29th, making it 366 days long instead of the usual 365 days. The rule for determining leap years is as follows:
-> Years divisible by 4 are leap years, except for years that are divisible by 100.
-> However, years divisible by 400 are also leap years.
In this tutorial, we’ll develop a Python program to check if a given year is a leap year or not.
Developing the Algorithm
-> Take user input for the year to be checked.
-> Check if the year satisfies the leap year conditions mentioned above.
-> Display the result whether the given year is a leap year or not.
Writing the Python Code
# Function to check if a year is a leap year
def is_leap_year(year):
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
return True
return False
# Main function
def main():
# Input year to be checked
year = int(input("Enter a year: "))
# Check if the year is a leap year
if is_leap_year(year):
print(year, "is a leap year.")
else:
print(year, "is not a leap year.")
if __name__ == "__main__":
main()
O/P
Enter a year: 2024
2024 is a leap year.
Time Complexity
The time complexity of this program is constant (O(1)) because the execution time is not dependent on the input value. The program will always execute the same number of operations regardless of the year entered.
Code Explanation
We define a function is_leap_year that takes a year as an argument.
Inside the function, we check whether the year satisfies the leap year conditions using the given formula.
If the conditions are met, the function returns True; otherwise, it returns False.
The main function is defined to take user input for the year to be checked.
User input is converted to an integer using int(input(…)).
The is_leap_year function is called with the user-provided year, and the result is stored in the is_leap variable.
Based on the result, the program prints whether the year is a leap year or not.
Dry Run
Let’s perform a dry run of the program with the following input:
Year = 2024
Execution:
User enters the year: 2024
The is_leap_year function is called with year = 2024.
The conditions 2024 % 4 == 0 and 2024 % 100 != 0 are satisfied, so the function returns True.
The program prints “2024 is a leap year.”
The program correctly identifies the year 2024 as a leap year based on the leap year conditions.
