► Python program to print fibonacci series of given length


A sequence is called Fibonacci sequence if each number is the sum of the two preceding ones, starting from 0 and 1.

Steps followed:
1. take length of fibonacci series as input from user
2. validate user input by converting to integer
3. print fibonacci series using for loop
 


Program

#python program to print fibonacci series of given length

#take length of fibonacci series as input from user
length=input("length of fibonacci series: ")

try:
    length=int(length)
    i = 0
    j = 1

    print(i, end = " ")
    print(j, end = " ")
    
    #print fibonacci series
    for x in range(0,length-2):
        step = i + j
        print(step, end = " ")
        i = j
        j = step
        
except:
    print("---error---")
    print("please enter a valid length")


Output

length of fibonacci series: 10
0 1 1 2 3 5 8 13 21 34

 

length of fibonacci series: 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 





Also Read: