Python exercise: Leap Year Calculator

Exercise: Leap Year Calculator

This program calculates whether a given year is a leap year. A normal year has 365 days, leap years have 366, with an extra day in February. While you can always look up a list for this info (https://www.thelists.org/list-of-leap-years.html), coding it is useful for a number of applications where dates are involved.

This is the logic of how to determine if a particular year is a leap year.
>>> If the year is cleanly divisible (i.e. no remainder) by 4, but not by 100, it's a leap year. Otherwise, it's not.
>>> If the year is cleanly divisible by 4, and by 100, and by 400, it's a leap year. Otherwise it's not.

leap_year_calculator.py
-------------------------------

year = int(input("Which year do you want to check? "))

if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print("Leap year")
        else:
            print("Not leap year")
    else:
        print("Leap year")
else: 
    print("Not leap year")

-------------------------------
Previous
Previous

Python exercise: Pizza Cost Calculator

Next
Next

Python exercise: BMI Calculator