Write a Program to Check Whether a Year is Leap Year or Not in Python

Hi, in this article lets learn Program to Check Whether a Year is Leap Year or Not in Python, 

A Leap Year comes every fourth year, Leap Year has 366 days i.e, February 29 is added to the Leap Year. Non Leap Year has normal 365 days and moth of February has only 28 days.

 

Write a Program to Check Whether a Year is Leap Year or Not in Python

 


Given a number it is said to be leap year if it satisfies either of these two conditions.

Condition #1 : Completely Divisible by 4 and not divisible by 100

(OR)

Condition #2 : Completely Divisible by 400

Now lets use the above logic to write a program to check leap year in python.


Program to Check Whether a Year is Leap Year or Not in Python

Lets take a variable year and ask user to enter the year, convert it to integer. Once we get year use the above two conditions year % 4 == 0 and year % 100 != 0 (or) year % 400 == 0. Check if either of these two condition is true, if so print its Leap Year else not a leap year

Python Program : 

year = int(input("Enter year : "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print("Year you entered is Leap Year")
else:
    print("Year you entered is Not a Leap Year")

 

OUTPUT 1 :

# python main.py
Enter year : 23
Year you entered is Not a Leap Year


OUTPUT 2 :

# python main.py
Enter year : 44
Year you entered is Leap Year

OUTPUT 3 :

# python main.py
Enter year : 2022
Year you entered is Not a Leap Year

 

Conclusion : Comment down below if you have any queries on the above program.



Post a Comment

0 Comments