In this article, we will discuss how to write a python program to check whether a given year is a leap year or not.
Firstly, let's discuss "What is Leap year?".
A year is a Leap year if it is exactly divisible by 4 except for the years ending with double zero(00) (also called century years). And among the century years, only those years are the leap year which is perfectly divisible by 400.
I will be discussing 3 different ways to write the python program for it.
- Using multiple if-else statements
- Using single if-else statement
- And by using if-elif statement
Let's discuss all the ways one by one.
Python Program to Check Whether a given Year is a Leap Year or not
Using multiple if-else statements
In this method, we will be using multiple if-else statements.
I have also used format() function so that I would be able to print the output in a formatted way.
In this program, the {0} is replaced with the variable passed inside the parameters of format() function. In this example, the 0 inside the braces({}) represents the parameter number to be replaced with.
Code:-
# taking year in YYYY format
year = int(input("Enter the year: "))
# checking whether year is divisible by 4
if(year%4 == 0):
# checking whether year is divisible by 100
if(year%100 == 0):
# checking whether year is divisible by 400
if(year%400 == 0):
# printing the year is leap year
print("{0} is a leap year.".format(year))
else:
# printing the year is not a leap year
print("{0} is not a leap year.".format(year))
else:
# printing the year is leap year
print("{0} is a leap year.".format(year))
else:
# printing the year is not a leap year
print("{0} is not a leap year.".format(year))
Output:-
Enter the year: 2016
2016 is a leap year.
Using single if-else statement
We can shorten the above program by using just single if-else statement.
Here also I have used the format() function.
Code:-
# taking year in YYYY format
year = int(input("Enter the year: "))
# checking whether year is leap year or not
if year%400 == 0 or year%4 == 0 and year%100 != 0:
print("{0} is a leap year.".format(year))
else:
print("{0} is not a leap year.".format(year))
Output:-
Enter the year: 2100
2100 is not a leap year.
Using if-elif statement
Here, we will be discussing the use the if-elif statement. The elif is short for else-if.
Code:-
# taking year in YYYY format
year = int(input("Enter the year: "))
# checking whether year is divisible by 400
if(year%400 == 0):
# printing the year is leap year
print("{0} is a leap year.".format(year))
# checking whether year is divisible by 100
elif(year%100 == 0):
# printing the year is not a leap year
print("{0} is not a leap year.".format(year))
# checking whether year is divisible by 4
elif(year%4 == 0):
# printing the year is leap year
print("{0} is a leap year.".format(year))
else:
# printing the year is not a leap year
print("{0} is not a leap year.".format(year))
Output:-
Enter the year: 1300
1300 is not a leap year.
Hence, we have discussed all the ways.
We have learned different ways to use the if-else statements.
And also we have learned about the use of format() function.