Python Program to Check Leap Year

Problem Description

Write a Python program that takes a year as input and checks whether it is a leap year or not.

What is Leap Year?

A leap year is a year that is divisible by 4 but not by 100, unless it is also divisible by 400. Leap years have an extra day, February 29th, instead of the usual 28 days.

How to check whether it is a Leap year or not?

To check if a year is a leap year, it should satisfy the following conditions:

  • The year should be divisible by 4.
  • If the year is divisible by 100, it should also be divisible by 400.
Examples:

Example 1: Let’s check if the year 2024 is a leap year using these conditions:

  • Is 2024 divisible by 4? Yes, it is.
  • Is 2024 divisible by 100? No, it is not.

Therefore, 2024 is a leap year.

Example 2: Let’s take another example to determine if the year 2100 is a leap year or not:

  • Is 2100 divisible by 4? Yes, it is.
  • Is 2100 divisible by 100? Yes, it is.
  • Is 2100 divisible by 400? No, it is not.

Therefore, 2100 is not a leap year.

advertisement
advertisement
Method 1: Find Leap Year using Multiple else-if Statements

Here is source code of the Python Program to check whether a given year is leap year using single if else statement. The program output is also shown below.

# Python Program to Check Leap Year
 
year=int(input("Enter year to be checked:"))
if(year%4==0 and year%100!=0 or year%400==0):
    print("The year is a leap year!")
else:
    print("The year isn't a leap year!")
Program Explanation

1. User must first enter the year to be checked.
2. The if statement checks if the year is a multiple of 4 but isn’t a multiple of 100 or if it is a multiple of 400 (not every year that is a multiple of 4 is a leap year).
3. Then the result is printed.

Time Complexity: O(1)
The time complexity of the leap year program in Python is O(1) because the execution time remains constant regardless of the input size.

Space Complexity: O(1)
The space complexity of the leap year program is also O(1) since the amount of memory used by the program remains constant, regardless of the input size.

Runtime Test Cases

Testcase 1: Here is the runtime output of a Python program to find the leap year when the year entered by the user is “2024”.

Enter year to be checked:2024
The year is a leap year!

Testcase 2: In this case, we enter the year “2100” as input to check whether a given year is leap year or not.

advertisement
Enter year to be checked:2100
The year isn't a leap year!
Method 2: Find Leap Year using Multiple else-if Statements

Here is source code of the Python Program to check whether a given year is leap year using Multiple if else statements. The program output is also shown below.

year = int(input("Enter year to be checked: "))
 
if year % 4 == 0:
    if year % 100 == 0:
        if year % 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!")
Program Explanation

1. This program takes a year as input and uses nested if-else statements to determine if it is a leap year or not.
2. It checks if the year is divisible by 4, then further checks if it is divisible by 100 and 400 to make the final leap year determination.

advertisement
Program Output:

Here is the runtime output of a Python program to find the leap year when the year entered by the user is “2012”.

Enter year to be checked: 2012
The year is a leap year!

To practice programs on every topic in Python, please visit “Programming Examples in Python”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.