codescracker
python

Python Program to Check Leap Year



« Previous Program Next Program »

In this article, we've created some programs in Python, to check whether a year entered by user at run-time is a leap year or not. Here are the list of programs on checking leap year through Python:

But before going through these programs, let's me clear the thing about leap year.

Condition for Leap Year

Any year can be called as a leap year, only if:

Note - If you're curious to understand about its logic, then refer to Leap Year Logic to get every required thing about it.

Check Leap Year

To check whether a given year is a leap year or not in Python, you have to ask from user to enter a year, then check and print the message whether it is a leap year or not as shown in the program given below. The question is, write a Python program to check whether a given year by user, is a leap year or not. Here is its answer:

print("Enter the Year: ")
y = int(input())

if y%4==0 and y%100!=0:
    print("\nIt is a Leap Year")
elif y%400==0:
    print("\nIt is a Leap Year")
else:
    print("\nIt is not a Leap Year")

Here is the initial output produced by this Python program:

check leap year python

Now supply the input say 2012 as year and press ENTER key to check whether it is a leap year or not, and print the message as shown in the snapshot given below:

check leap year or not python

Here is another sample run with user input, 1900 as year:

python check leap year

Modified Version of Previous Program

This is the modified version of previous program. Here we've used end, that skips printing of an automatic newline using print(). The method, str() is used to convert any type of value to a string type, so that all the string inside print() gets concatenated using + operator:

print("Enter the Year: ", end="")
y = int(input())

if y%4==0 and y%100!=0:
    print("\n" +str(y)+ " is a Leap Year")
elif y%400==0:
    print("\n" +str(y)+ " is a Leap Year")
else:
    print("\n" +str(y)+ " is not a Leap Year")

Here is its sample run with user input, 2000:

python check leap year

Check Leap Year using Function

This program does the same job as of previous program. The only difference is, this program uses a user-defined function named checkLeapOrNot(). This function receives year's value entered by user as its argument. It returns 1 if year is a leap year.

def checkLeapOrNot(x):
    if y % 4 == 0 and y % 100 != 0:
        return 1
    elif y % 400 == 0:
        return 1

print("Enter the Year: ", end="")
y = int(input())

chk = checkLeapOrNot(y)
if chk==1:
    print("\n" +str(y)+ " is a Leap Year")
else:
    print("\n" +str(y)+ " is not a Leap Year")

This program produces similar output as of previous program.

Check Leap Year using Class

This program uses class named CodesCracker, an object-oriented feature of Python to do the same job as of previous program. Since to access any member function of the class, we've to use its object. Therefore we've created an object named ob. Let's have a look at the program given below to understand in better way:

class CodesCracker:
    def checkLeapOrNot(self, x):
        if y % 4 == 0 and y % 100 != 0:
            return 1
        elif y % 400 == 0:
            return 1

print("Enter the Year: ", end="")
y = int(input())

ob = CodesCracker()
chk = ob.checkLeapOrNot(y)
if chk==1:
    print("\nLeap Year")
else:
    print("\nNot a Leap Year")

Here is its sample run with user input 2014:

python check leap year using class

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker