Leap Year Program in Python using if-else

We will code Leap Year Program in Python using if-else and functions. We will also provide hackerrank solution for leap year program.

 In this post, we will make a python program to check whether the given year is a leap year.

Leap Year: One year in every four has 366 days [February has 29 days instead of 28]. The reason why we have leap years is really fascinating, this video does more justice: Video Link

Algorithm:

If one of the following conditions is satisfied then the given year is a leap year.
  • Every year that is divisible by 4 and not divisible by 100.
  • A year that is divisible by 4 and also divisible by 400.

Leap Year Program in Python using if-else

In this program, we will check for leap year using simple if-else conditions. We will take input from the user and typecast it to int and then check whether the given year is a leap year or not using the above algorithm.
year = int(input("Which year do you want to check? "))

if (year%4==0 and year%100!=0) or (year%4==0 and year%400==0):
    print("Leap year.")
else:
    print("Not leap year.")

Output:

Which year do you want to check? 2022
Not leap year.

leap year program in python hackerrank solution

In this program, we will code answers for the leap year question at hackerrank. We completed the is_leap function and it will return true when the given year is a leap year and false otherwise.

leap year program in python hackerrank solution

def is_leap(year):
  leap = False

  # Write your logic here
  if (year%4==0 and year%100!=0) or (year%4==0 and year%400==0):
      leap=True
  return leap

year = int(input())
print(is_leap(year))
I hope you like this post, if you like this post please comment and if you are new to our blog please subscribe to our newsletter. Thank you so much for reading.

If you are looking for jobs/internships, you may join our telegram for the latest jobs and internships updates.

4 comments

  1. woowww... what a content
    never seen before like this :)
  2. Great content
    Really works...
    1. Thanks 🙏
  3. My Code:

    year = int(input("Which year do you want to check?"))

    if year % 4 == 0:
    if year % 100 == 0:
    if year % 400 == 0:
    print("Leap year.")
    else:
    print("Not leap year.")
    else:
    print("Leap year.")
    else:
    print("Not leap year.")

Please do not enter any spam link in the comment box.