Leap Year Program in Python

Leap Year Program in Python

The objective of this Python post, you will see various Python examples that cover the following:

Now let’s see each one by one:

1: Python program to check whether a given year is a leap year or not

Algorithm to check whether the given year is a Leap Year or not in Python using if, elif statements:

1: Leap year program in python using if statement:

  • Use a python input() function in your python program that allows the user to enter any year.
  • Next, use if statement to check whether the user entered year is Leap year or not. And store the result in a variable.
  • At the end of the program, print results.
# Python program to check the given year is leap or not

n = input("Please enter year")

year = int (n)

if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):
    print("%d is a Leap Year" %year)
else:
    print("%d is Not the Leap Year" %year)

Output

Please enter year 2021 
2021 is Not the Leap Year 

2: Leap year program in python using elif:

  • Use a python input() function in your python program that allows the user to enter any year.
  • Next, use elif statement to check whether the user entered year is Leap year or not. And store the result in a variable.
  • At the end of the program, print results.
# Python program to check the given year is leap or not

n = input("Please enter year")

year = int (n)

if (year%400 == 0):
          print("%d is a Leap Year" %year)
elif (year%100 == 0):
          print("%d is Not the Leap Year" %year)
elif (year%4 == 0):
          print("%d is a Leap Year" %year)
else:
          print("%d is Not the Leap Year" %year)

Output

Please enter year 2020 
2020 is a leap year 

3: leap year program in python using nested if

  • Use a python input() function in your python program that allows the user to enter any year.
  • Next, use nested if statement to check whether the user entered year is Leap year or not. And store the result in a variable.
  • At the end of the program, print results.
# Python program to check the given year is leap or not

n = input("Please enter year")

year = int (n)

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

Output

Please enter year 2020 
2020 is a leap year 

2: Python program to check leap year using function

Algorithm to check whether the given year is a Leap Year or not in Python using Function:

  • Use a python input() function in your python program that allows the user to enter any year.
  • Next, import the math calendar module.
  • By using isleap() function of calendar module, To check whether the given year is a leap year or not.
  • After that, print the result.
# Python program to check the given year is leap or not 
# using math calender module in Python

#here import calender module
import calendar

n = input("Please enter year")

year = int (n)

# calling isleap() method to check for Leap Year
val = calendar.isleap(year)

if val == True: 
  print("% s is a Leap Year" % year)  
else:
  print("% s is not a Leap Year" % year)

Output

Please enter year 2019 
2019 is not a Leap Year  

Leave a Reply

Your email address will not be published. Required fields are marked *