Python Program to Check if a Year is a Leap Year
In this tutorial, we are going to see how to write a Python program to check if the input year (entered by the user) is a leap year or not.
You can check if a year is a leap year or not using this mathematical logic:
Leap Year:
- If a year is divisible by 4, 100 and 400, it is a leap year.
- If a year is divisible by 4 but not by 100, it is a leap year.
Not a leap year:
- If a year is not divisible by 4, it is not a leap year.
- If a year is divisible by 4 and 100 but not by 400, it is not a leap year.
Python Program to Check if a Year is a Leap Year
In this program, the user is prompted to enter a year. The program checks if the entered year is a leap year or not.
year = int(input("Enter the year to verify:")) if(year%4==0 and year%100!=0 or year%400==0): print("The year is a leap year!") else: print("The year is not a leap year!")
Output:
Enter the year to verify: 2016 The year is a leap year! Enter the year to verify: 2005 The year is not a leap year!
- The user must first enter the year to be checked.
- The if statement checks if the year is a multiple of 4 but not a multiple of 100 or if it is a multiple of 400 (because not every year, a multiple of 4 is a leap year).
- The result is then displayed.