Python Program To Generate Fibonacci Series

Python Program To Generate Fibonacci Series

This article features a Python Program to generate the Fibonacci series using the if-else statement and while loop upto n number of terms.

Fibonacci Series

The Fibonacci series is a sequence in which each number is the sum of the previous two numbers. The few terms of the simplest Fibonacci series are 1, 1, 2, 3, 5, 8, 13 and so on. The nth number of the Fibonacci series is called Fibonacci Number and it is often denoted by Fn. For example, the 6th Fibonacci Number i.e. F6 is 8.

Python Program To Generate Fibonacci Series

The logic behind this sequence is quite easy. Each new item of series can easily be generated by simply adding the previous two terms. But before we can do so, we must store the previous two terms always while moving on further to generate the next numbers in the series. There are different logics and approaches that can be used to generate the Fibonacci series in python.

The following program is the simplest way that makes use of the if-else statement in association with the while loop.

Output.

Python Program To Generate The Fibonacci Series

After the program runs, it asked for the number of terms required in the series and I entered 10 and hence it generated the Fibonacci Series involving the first 10 terms. Note that 0 is also included in this series. You may or may not consider 0 as the first number of your Fibonacci Series and accordingly you might need to slightly modify the code but the logic will remain the same. It’s just that you have to change the value for the variables first_term and second_term, both to 1 instead of 0 and 1.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *