Python Program to check whether the Year is Leap Year or Not
Write a Python program that has a method called is_leap_year() which accepts the year as its argument and checks whether the year is a leap year or not and displays an appropriate message on the screen.
Video Tutorial:
Logic of Leap year program:
if the year is divisible by 400 then the year is a leap year
Example: 1600, 2000, 2400, and so on
if the year is divisible by 4 but not by 100 is a leap year
Example:
Leap Year: 2004, 2008, 2012, 2016, 2020, and so on
Not Leap Year: 1800, 1900, 2100 are not leap years
Steps:
1. First we, define the is_leap_year() method, which accepts an argument say a year.
2. Next, we check whether the year is a leap year or not a leap year.
2.1. If Year is a leap year, display a message as the year is a leap year.
2.2. If Year is not a leap year, display a message as the year is not a leap year.
3. Next, read the year from the user and pass the year to the is_leap_year() method.
Source code of Python Program to check whether the Year is Leap Year or Not
def is_leap_year(year): if ((year % 4 == 0 and year % 100 != 0) or year % 400 ==0): print (year, "is a leap year") else: print (year, "is not a leap year") year = int (input("Enter the year: ")) is_leap_year(year)
Output:
Case 1:
Enter the year: 2000
2000 is a leap year
Case 2:
Enter the year: 1600
1600 is not a leap year
Case 3:
Enter the year: 2020
2020 is a leap year
If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.