Python Program to Check Leap Year or not

Write a Program to Check Leap Year or not

Python Examples-InterviewGIGLeap Year in Mathematics:

In the Gregorian calendar system, a leap year is a year that is evenly divisible by 4, except for end-of-century years. However, years that are divisible by 100 are not leap years, unless they are also divisible by 400. This additional rule helps keep the calendar year in sync with the solar year. For example, the year 2000 was a leap year because it is divisible by both 4 and 400, but the year 1900 was not a leap year because it’s divisible by 4 and 100 but not 400.

Python Program to Check Leap Year:

Here’s a Python program that checks whether a given year is a leap year or not:

year = int(input("Enter a year: "))
is_leap = False
if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            is_leap = True
    else:
        is_leap = True
print("Leap Year" if is_leap else "Not a Leap Year")

0r

year = int(input("Enter a 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.")

Explanation:

  • The program starts by taking an integer input from the user using the input() function and converting it into an integer using the int() function. This input is stored in the variable year.
  • The variable is_leap is initialized to False, assuming that the year is not a leap year initially.
  • The program uses conditional statements to check whether the year is a leap year:
  • If the year is divisible by 4, the program enters the first level of conditional statements.
  • If the year is divisible by 100, it enters the second level of conditional statements.
  • If the year is also divisible by 400, it’s a leap year, and is_leap is set to True.
  • If the year is not divisible by 100, it’s a leap year, and is_leap is set to True.
  • After the nested conditionals, the program prints “Leap Year” if is_leap is True, indicating that the year is a leap year. Otherwise, it prints “Not a Leap Year“.

Output:

Output (Leap Year):

Enter a year: 2024
2024 is a leap year.

In this example, the user enters the year 2024. Since 2024 is divisible by 4 and not divisible by 100, the program determines that it’s a leap year and displays “2024 is a leap year.”

Output (Non-Leap Year):

Enter a year: 2021
2021 is not a leap year.

In this example, the user enters the year 2021. Since 2021 is not divisible by 4, the program determines that it’s not a leap year and displays “2021 is not a leap year.”

 

Scroll to Top