Write a Python program that takes a year as input and checks whether it is a leap year or not.
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.
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.
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.
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!")
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.
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.
Enter year to be checked:2100 The year isn't a leap year!
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!")
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.
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”.
- Apply for Python Internship
- Check Python Books
- Practice Programming MCQs
- Apply for Programming Internship
- Check Information Technology Books