Fibonacci sequence

Fibonacci sequence are the series of numbers where each number is addition of previous two numbers.It starts with 0 and 1. So the third number is 0+1=1 and the fourth number is 1+1=2. 

Following is an example of the series,

 

0, 1, 1, 2, 3, 5, 8, 13, 21, ...

 

Let us write an python program to generate the series.

  

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
lenghtofseries = int(input("Please enter the length of the series "))

a, b = 0, 1
count = 0

if lenghtofseries <= 0:
   print("Length of the must be a positive number")
elif lenghtofseries == 1:
   print(a)
else:
   print("Fibonacci sequence:")
   while count < lenghtofseries:
       print(a)
       c = a + b
       a = b
       b = c
       count += 1  


The output of the program is,

Please enter the length of the series 11
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
55

Comments

Popular posts from this blog

IDEs for Python Beginners