Vipin | Thu, 04 Jun, 2020 | 309

Fibonacci series is a series of numbers formed by the addition of the preceeding two numbers in the series.

**Example of Fibonacci Series:** 0,1,1,2,3,5

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

F_{n} = F_{n-1} + F_{n-2}

with seed values

F_{0}= 0 and F_{1}= 1.

**1. Using loop**

```
# Enter number of terms needed #0,1,1,2,3,5....
a=int(input("Enter the terms")) # number of terms to be printed
f=0 #first element of series
s=1 #second element of series
if a<=0:
print("The requested series is
",f)
else:
print(f,s,end=" ")
for x in range(2,a):
next=f+s
print(next,end=" ")
f=s
s=next
```

**output:**

Enter the terms 5

0 1 1 2 3

**2. Using Recurrsion**

```
def FibRecursion(n):
if n <= 1:
return n
else:
return(FibRecursion(n-1) + FibRecursion(n-2))
nterms = int(input("Enter the terms? ")) # take input from the user
if nterms <= 0: # check if the number is valid
print("Please enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(FibRecursion(i))
```

**Output:**

** **How many terms: 5

0 1 1 2 3

**Explanation:**

** **In the above Python program, we use recursion to generate the Fibonacci sequence. The function FibRecursion is called recursively until we get the output. In the function, we first check if the number n is zero or one. If yes, we return the value of n. If not, we recursively call fibonacci with the values n-1 and n-2.