# Python program to find Fibonacci series

Python program to find Fibonacci series

A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence that has integers 0 and 1 at first and second place, and the rest of the sequence term will be the addition of their two previous integers.

Source Code

The following is the source code to display the Fibonacci series:

``` #Program to print Fibonacci series
#Take user input to print number of terms
nterms = int(input("How many terms we want:"))
#Fisrt two term of sequence
t1 = 0
t2 = 1
count = 0
#Check the validation of number of terms
if nterms <= 0:
print("Enter positive element")
elif nterms == 1:
print("Fibonacci series upto",nterms,":")
print(t1)                                                                                                 else:
print("Fibonacci sequence:")
while count < nterms:
print(t1)
nth = t1 + t2
#Update values
t1 = t2
t2 = nth
count += 1 ```

Output

Here is the result:

Explanation: In this program, we have first declared the variable nterms that will store the number of terms to display the Fibonacci series. Next, we need to initialize the first term to 0 and the second term to 1. Whenever the number of terms is more than 2, a while loop is executed to find the next term of the series by adding the previous two values. Next, we will swap the variable, update it, and continue this process to the final terms. Finally, it will display the result on the screen.

Using Recursion

The following is a source code to get the Fibonacci sequence using recursion:

``` #Program to print Fibonacci series using recursion
#Create a function to return nth terms of the sequence
def recursionFibonacci(n):
if n <= 1:
return n
else:
return(recursionFibonacci(n-1) + recursionFibonacci(n-2))
# take input from the user
nterms = int(input("How many terms we want:"))
# check if the number of terms is valid
if nterms <= 0:
print("Enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recursionFibonacci(i))   ```

Output

Here is the result:

Explanation: In this program, we have first declared the variable nterms that will store the number of terms to display the Fibonacci series. Next, calculate the nth term of the sequence using a recursive function recursionFibonacci(). Then, we use the loop for iteration and calculate each term recursively. Finally, it will display the result on the screen.