Write a Python program to check whether a given year is a leap year or not.
Input-Output Examples
plaintext
Example 1:
Input: Enter a year: 2024
Output: 2024 is a leap year.
Example 2:
Input: Enter a year: 2023
Output: 2023 is not a leap year.
Algorithm to check whether a given year is leap year or not
- Start
- Take input: Prompt the user to enter a year.
- Check the year:
- If the year is divisible by 4:
- If the year is divisible by 100, check if it is also divisible by 400. If yes, it is a leap year.
- Otherwise, it is a leap year.
- If not divisible by 4, it is not a leap year.
- If the year is divisible by 4:
- Display the result: Print whether the year is a leap year or not.
- End
Python Program to check whether a year is leap year or not
plaintext
# Python program to check if a year is a leap year
# Take input from the user
year = int(input("Enter a year: "))
# Check if the year is a leap year
if (year % 4 == 0):
if (year % 100 == 0):
if (year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
else:
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Code Explanation
- Taking input from the user:
The program uses theinput()
function to take a year as input from the user. The input is converted to an integer usingint()
. - Checking if the year is a leap year:
The program checks whether the year is divisible by 4, 100, and 400 using conditional statements:- If the year is divisible by 4, it may be a leap year.
- If it is divisible by 100, the program checks if it is divisible by 400 as well. If both conditions are satisfied, it is a leap year; otherwise, it is not.
- If it is divisible by 4 but not by 100, it is a leap year.
- Displaying the output:
Theprint()
function displays whether the year is a leap year or not using an f-string to format the output.