How to Program to Check Leap Year in python

Published on Aug. 22, 2023, 12:16 p.m.

To check if a year is a leap year in Python, you can use the following program:

year = int(input("Enter a 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, we first prompt the user to enter a year using the input() function, and then convert the input to an integer using the int() function. We then use a nested if-else statement to check if the year is a leap year. According to the algorithm, a year is a leap year if it is divisible by 4 but not divisible by 100, or if it is divisible by 400. We use the modulo operator % to check if the year is divisible by 4, 100, or 400.

This program will print whether the given year is a leap year or not based on the input provided by the user.

Note that the algorithm to determine leap year status might vary in different calendars, but this program applies to Gregorian calendar.

Tags:

related content