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 *F _{n}*. For example, the 6th Fibonacci Number i.e.

*F*is 8.

_{6 }## 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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# Python Program to Generate Fibonacci Series Upto n Terms #Ask for number of terms the user wanted in the generated series n = int(input("Number of Terms Required in Series: ")) #Defining the First Two Terms first_term, second_term = 0, 1 #Count Variable i = 0 #Logic to check if the user enetered a positive Integer if n <= 0: print("Only Positive Integers Allowed.") elif n == 1: #In case user enters 1, print only the first term print("Fibonacci sequence upto",nterms,":") print(first_term) else: print("Required Fibonacci Series: ") while i < n: print(first_term) next_term = first_term + second_term #Updating Values For Next Iteration first_term = second_term second_term = next_term #Increasing the Count Value by 1 i += 1 |

**Output.**

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!