This article will show you how to write a python program to check leap year. The program will read year as input and find out whether it is a leap year or not.
What is a leap year?
A normal year has 365 days but when an year has 366 days, it is said to be a leap year. A leap year comes once in 4 years.

Leap year logic
In order to determine whether a given year is a leap year or not, following steps are followed.

  1. Divide the year by 4. If the remainder is 0(or the year is completely divisible by 4), then the year may be a leap year but if the year is not divisible by 4, then it is not a leap year and you can directly skip the next steps.
  2. Check if the year is divisible by 100. If
    1. the year is divisible by 100, then divide the year by 400. If the year is also divisible by 400, then it is a leap year else not.
    2. the year is not divisible by 100, but divisible by 4, then it is a leap year.

Generally, second step check is applicable to only century years, those that end with 00. Examples,
2000 is a leap year. Divisible by 100 and 400 both.
2300 is not a leap year. Divisible by 100 but not by 400.
1900 is not a leap year

Python program
Python program to check for a leap year based on the above logic is given below.

# read year from keyboard
year = int(input('Enter year\n'))
# check if year is divisible by 4
if(year % 4) == 0:
    # check if year is divisible by 100
    if(year %100) == 0:
        # check if year is divisible by 400
        if(year % 400) == 0:
            print("Leap year")
        else:
            # year is divisible by 100 but not by 400
            print("Not Leap year")
    else:
        # year is divisible by 4 but not by 100
        print("Leap year")
else:
    # year is not divisible by 4
    print("Not Leap year")

 

Above python program will first check if the entered year is divisible by 4. If it is, then it will check the divisibility with 100.
If the year is not divisible by 100, then it is a leap year(divisible by 4).
If the year is divisible by 100, then its divisibility with 400 is checked.
As stated earlier, divisibility with 100 is required only for century years.

Output of above program for different executions is

Enter year
1996
Leap year
Enter year
2100
Not Leap year

Above condition to check for leap year can also be modified as one-liner below

if((year % 4) == 0 and (year % 100 != 0)) or (year % 400 ==0):
     print("Leap year")
else:
    # year is not divisible by 4
    print("Not Leap year")

This condition is made up of two parts.
A. It checks if the year is divisible by 4 and not by 100 which will return True for all non-century years such as 1996, 1992, 1880 etc.
B. It checks if the year is divisible by 400. It will also return True if the years are divisible by 100 because any year divisible by 400 will anyway be divisible by 100 as well.
It will cover all the century years such as 1600, 2000, 2400 etc.

If any of the above condition is met, the entered year will be a leap year.
Click the clap icon below if the article was useful.

Leave a Reply