Pythondex
Python Programs

Python Program To Find Next 15 Leap Years And N Leap Years

Last updated May 1, 2023 by Jarvis Silva

Want to know the next 15 leap years using python, then you are at the right place, In this tutorial I will show you how to create a python program to find next 15 leap.

A normal year has 365 days but a leap year has 366 days because in a normal year February month has only 28 days but in a leap year February month has 29 days and a leap year comes after every four years.

Python Program To Find Next 15 Leap Years Code


def find_leap_years(year):
    count = 0
    leap_years = []
    
    while count < 15:
        if year % 400 == 0  or (year % 4 == 0 and year % 100 != 0):
            leap_years.append(year)
            count += 1
            year +=4
        else:
            print(f"{year} is not a leap year")
    return leap_years
    

y = int(input("Enter year: "))
leap_years_list = find_leap_years(y)

print(leap_years_list)

Above is the code to find the next 15 leap years, in the code we ask the user to enter a year and it will print the next 15 years by following the below criterias:

  • If the year is divisible by 4 with remainder 0 then next step, If not divisible by 4 then it is not a leap year.
  • Now if the year is divisible by 4 and not by 100 then it is a leap year. If the year is divisible by both 4 and 100 then next step.
  • If the year is divisible by 4 and 100 but not by 400 then it is not a leap year, if it is divisible by all 3 then it is a leap year.

It will check this for 15 times in a while loop, then it will append the leap year in a list which contains the list of all found leap years.

Now you can run this program on your computer, If you don’t have python installed follow this guide: Install and setup python or else you use this online python compiler.

After running it asks you to enter the year you want to find the leap years from, then it will print a list of the next 15 leap years, Below is an example output of the next 15 leap years from 2000.


Enter year: 2000
[2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, 2048, 2052, 2056]

As you can see, it successfully prints the next 15 leap years now let's see how can find any number of leap years not just 15.

Python Program To Find N Leap Years Code

Now if you want the python program to find more then 15 leap years than you can do that by using the below code


def find_leap_years(year,years):
    count = 0
    leap_years = []
    
    while count < years:
        if year % 400 == 0  or (year % 4 == 0 and year % 100 != 0):
            leap_years.append(year)
            count += 1
            year +=4
        else:
            print(f"{year} is not a leap year")
    return leap_years
    


y = int(input("Enter year: "))
x = int(input("How many years you want: "))
leap_years_list = find_leap_years(y,x)

print(leap_years_list)

In this program, we just added an input statement asking how many years you want it to print, you can run this program by following the steps I mentioned earlier.

Summary

This was the tutorial on find leap years in python, I hope you found this tutorial helpful and useful. Do share this with your friends who might be interested in this python program.

Here are some more python tutorials that will interest you:

I hope you found what you were looking for from this python tutorial, and if you want more tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂