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

Want to know the next 15 leap years using python, then you are at the right place today. In this tutorial I will show you the python program to find next 15 leap years, so read till the end.
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. A leap year comes after every four years.
Today I will not only show you how to write a python program that will print the next 15 leap years but a python program to find n leap years, n means any number of leap years you want.
How to determine if a year is a leap year?
So we need some criterias on which we will decide if a given year is a leap year or not. To do that, we will use the below steps to determine:
- 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. For example 1993.
- 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.
Above are the steps that we need to check if a year is a leap year. We will check this for 15 times in a while loop, then we will append the leap year in a list. Now let’s see it in code.
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 python code to find the next 15 leap years. Now you have the code you can run this program on your computer, If you don’t have python installed follow this guide: Install and setup python.
To run this python leap year program, follow the below steps:
- Create a new folder for this atm python project.
- Open it in a code editor of your choice.
- Create a python file with an ending .py extension.
- Copy the above code and paste it in your file.
Now you have the code and you are ready to run this program, so open a command prompt or terminal at the project folder location and paste the below command to run.
python filename.py
The above command will run this program and it asks you to enter the year you want the leap years from, then it will print a list of the next 15 leap years. Below is the output of the next 15 leap years from 2000.
Output
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 in python. I hope you were able to run this program successfully.
Python Program To Find N Leap Years Code
Now if you want the python program to find next 5 leap years or more than you can do that by using the below code. Below python program prints next 5 leap years from 2000
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.
If you don’t have python installed or you are using a mobile, you can run this program using this online python compiler.
Summary
This was the python program to find next 15 leap years and n years. 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:
- Python program to create the matrix effect with code.
- Create vending machine program in python.
- Create ATM Program in python with source code.
- Create Joke Generator In Python With Code
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 🙂