Teaser-2 (Leap year in Python)

2.Write a function that determines if a given year is a leap year.

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.

Steps:

  1. If the year is divisible by 4, go to step 2. Otherwise, it is not a leap year.
  2. If the year is divisible by 100, go to step 3. Otherwise, it is a leap year.
  3. If the year is divisible by 400, it is a leap year. Otherwise, it is not a leap year.

year = int(input(“Enter a year to check if it’s a leap year: “))

if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
             print(f”{year} is a leap year.”)
        else:
            print(f”{year} is not a leap year.”)
   else:
        print(f”{year} is a leap year.”)
else:
    print(f”{year} is not a leap year.”)

Leave a Reply