Leap Year Program in Python

Written by

Himani Kohli

Leap year in python program

In this program, we are going to test whether a given year is a leap year or not.

A leap year is a year that is divisible by 4, except for years that are divisible by 100 but not by 400.

In other words, a leap year is a year that meets the following criteria:

  • It is divisible by 4.
  • It is not divisible by 100, or it is divisible by 400.

The century year is the leap year only if it is perfectly divisible by 400. 

For example:

2017 is not a leap year.

2000 is a leap year.

Algorithm:

  • Prompt the user to enter a year.
  • Store the year in a variable year.
  • Check if year is divisible by 4. If it is, go to step 4. If it is not, print "The year is not a leap year" and return from the function.
  • Check if year is divisible by 100. If it is, go to step 5. If it is not, print "The year is a leap year" and return from the function.
  • Check if year is divisible by 400. If it is, print "The year is a leap year". If it is not, print "The year is not a leap year".

Code:

n = int(input("Enter the year you want to check? "))

if n % 4 == 0:
  if n % 100 == 0:
    if n % 400 == 0:
      print("The year is a leap year")
    else:
      print("The year is not a leap year")
  else:
    print("The year is a leap year")
else:
  print("The year is not a leap year")

Output:

Enter the year you want to check? 1996
The year is a leap year

Enter the year you want to check? 1900
The year is not a leap year
Leap Year Program in Python