Sun Jul 25 2021

Fibonacci Series

File Name: fibonacci-series.py

#!/usr/bin/evn python

# Define function as 'F' and pass parameter as 'n'
def F(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        # Call function 'F' two times recursively by passing parameter 'n-1' and 'n-2' respectively and calculate sum
        return F(n-1)+F(n-2) 

# Take input from the user
num = int(input("Enter a number to generate Fibonacci sequence: "))
print("Fibonacci sequence:")
# For loop iterating by sequence index
for i in range(num):
    print(F(i));


#***** Output *****
# Enter a number to generate Fibonacci sequence: 10
# Fibonacci sequence:
# 0
# 1
# 1
# 2
# 3
# 5
# 8
# 13
# 21
# 34
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.