Robotics & Artificial Intelligence

The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The following program is designed to generate next five terms of the Fibonacci series, excluding 0 and 1 (i.e., first two terms of the series). The incomplete user-defined program is given as under:

def Fibo_Series():
    a = 0
    b = 1
    count = ........... # Line 1
    while(count <= ............):  # Line 2
        c = a + b
        print(.............)  # Line 3
        a = .............  # Line 4
        b = c
        count = count + 1
# main program
Fibo_Series()

Now, complete the above program to generate next five numbers of the series as 1, 2, 3, 5, 8.

Python Functions

1 Like

Answer

def Fibo_Series():
    a = 0
    b = 1
    count = 1  # Line 1
    while(count <= 5):  # Line 2
        c = a + b
        print(c)  # Line 3
        a = b  # Line 4
        b = c
        count = count + 1
# main program
Fibo_Series()

Explanation:

  • Line 1: count = 1 — initializes the counter to start from 1.
  • Line 2: count <= 5 — loop runs five times to generate five terms of the series.
  • Line 3: print(c) — displays the next Fibonacci number which is the sum of the two preceding terms.
  • Line 4: a = b — updates a with the previous value of b so that the next sum is calculated correctly.

Trace of values:

Iterationabc = a+bOutput
10111
21122
31233
42355
53588

Answered By

3 Likes


Related Questions