Leap Year Program Using If Else In Python

Leap Year Program Using If Else In Python
(Last Updated On: 19/09/2022)

Simply a leap year is a year with an extra day on February 29. Which is added nearly every four years to the calendar year. We use leap years to keep our calendar in sync with the seasons. How do leap years work, and how often do they occur? Leap years are years where an extra, or intercalary, the day is added to the end of the shortest month, February. The intercalary day, February 29, is commonly referred to as leap day.

Program to Leap Year Program Using If Else In Python

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

if(year % 4) == 0:
    if(year % 100) == 0:
        if(year % 400) == 0:
            print("{0} is a leap year". format(year))
        else:
            print("{0} is not a leap year". format(year))
    else:
        print("{0} is a leap year". format(year))
else:
    print("{0} is not a leap year". format(year))
Output:
Enter a Year:2000
2000 is a leap year

Code Explanation: Check Leap Year Using if Else in Python

Here we are accepting a year from the user, then converting the same into an integer and we have stored value in a variable year. Next, we have an if statement to check whether a year is divisible by 4  or not; here, we are using the modulus operator for it. If the year is divisible by 4, then we will have another if condition, else we will print year is not a leap year.

Do you remember 1st rules to calculate leap year? That year must be divisible by 4. Inside the condition, we have also checked if the year is divisible by 100 or not. If a year is divisible by 4 and not by 100, then it’s a leap year; else, it is not a leap year.

If the year is divisible by both 4 and 100, then as per the rule of leap year, we will check if the year is divisible by 400 or not. If the year is divisible by 400, then we have written a print statement stating that it is a leap year, and in the else section, we will print a year that is not a leap year.

Let’s run this program. The system will prompt you to enter the year; let’s enter the year 2000. The program will return in 2000 is a leap year. Again run the code and enter the year 2001; the program will print that 2001 is not a leap year.

If you want to learn python programming, you can refer to this Python Online Course with Certification.