Codeforcoding

Python Program to check leap year

Python Program to check leap year

In this article, we will discuss the concept of Python Program to find leap year

In this post, we are going to learn how to write a program to check and display  the given year is leap or not in  Python programming language.

Java program to check the given year is a leap year
leap year

What is leap year

leap year is occurring once every four years, which has 366 days including 29 February as an intercalary day. it is called leap year

Ex – 2000,2004.2008

 

Century year

The century year is ending with 00

Ex 2000, 2100, 2200

a century year may be a leap year(only if the year is exactly divisible by 400)

 

Code to check leap year

How to find the leap year

if The given year is perfectly divisible by 4 , it is a leap year(except century year)

the century year is a leap year, only it is perfectly divisible by 400..
a century year should be divisible by 4 and 100 both
a non century year should be divisible only by 4

Check if the given year is leap year using if else

Here, The program allows the user to enter the year and then it will check the given year is a leap year or not, using if – else statements in Python  language

Program 1

#Python program to check leap year using if statements
year=int(input("Please enter the year you wish: "));
if ((year%400==0)or((year%4==0)and(year%100!=0))):
    #if it is true display leap year
    #if it is false move to else part
     print("%d is a leap year"%year);
else:
     print("%d is not a leap year"%year);

When the above code is executed, it produces the following result

Case 1

Please enter the year you wish: 2224
2224 is a leap year

 

Case 2

Please enter the year you wish: 2222
2222 is not a leap year

 

Check if the given year is leap year using if elif  else

Here, The program allows the user to enter the year and then it will check the given year is a leap year or not, using if – elif elae statements in Python  language

Program 2

#Python program to check leap year using elif statements
year=int(input("Please enter the year for check leap or not: "))
#Ask input from the user and store the value in the year variable
if(year%400==0):
    #if the year id divided by 400, it is a leap year
       print("%d is a leap year"%year)
elif(year%100==0):
     #if the year id divided by 100, it is not a leap year
    #except divided by 400
       print("%d is not a leap year"%year)
elif(year%4==0):
    #if the year id divided by 4, it is a leap year
       print("%d is a leap year"%year)
else:
    print("%d is not a leap year"%year)
    #If the all statements are false it is not a leap

When the above code is executed, it produces the following result

Case 1

Please enter the year for check leap or not: 2300
2300 is not a leap year

 

Case 2

Please enter the year for check leap or not: 2204
2204 is a leap year

 

Check if the given year is leap year using Nested if

Here, The program allows the user to enter the year and then it will check the given year is a leap year or not, using Nested if elae statements in Python  language

Program 3

#Python program to check leap year using Nested if statements
year=int(input("Please enter the year for check leap or not: "))
#Ask input from the user and store the value in the year variable
if(year%4==0):
    if(year%100==0):
      if(year%400==0):
          #if the all statements are true
    # it is a leap year
       print("%d is a leap year"%year)
       #if the all statements are false
    # it is not a leap year
      else:
        print("%d is not a leap year"%year)

    else:
      print("%d is a leap year"%year)
      #it is a leap year

else:
    print("%d is not a leap year"%year)

When the above code is executed, it produces the following result

Case 1

Please enter the year for check leap or not: 2044
2044 is a leap year

 

Case 2

Please enter the year for check leap or not: 2050
2050 is not a leap year

 

Suggested post

if statements in Python  language

Nested if statements in Python language

Data type in Python language

Operator in Python language

 

Similar post

Java program to find leap year

C program to find leap year

C++ program to find leap year

Python program to find leap year

 

C++ program for check whether given year is leap using function

Python program for check whether given year is leap using function

C program for check whether given year is leap using function

 

Program to check if the given year is leap year in C++
C++ program for check whether given year is leap using function
Exit mobile version