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— updatesawith the previous value ofbso that the next sum is calculated correctly.
Trace of values:
| Iteration | a | b | c = a+b | Output |
|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 1 |
| 2 | 1 | 1 | 2 | 2 |
| 3 | 1 | 2 | 3 | 3 |
| 4 | 2 | 3 | 5 | 5 |
| 5 | 3 | 5 | 8 | 8 |
Answered By
1 Like
Related Questions
Predict the output of the following code:
def Disp_Max(a, b): if a > b: print(a, 'is maximum') elif a == b: print(a, 'is equal to', b) else: print(b, 'is maximum') Disp_Max(22, 14)Assertion (A): When the user defines a function of his/her own and use it in the program to perform a specific task, it is known as user defined function.
Reason (R): The types of functions viz. built-in functions and modules are also known as user defined functions because they are not defined by the system developers. Thus, it is not necessary that the system developers are only authorised for creating a function.
Based on the above discussion, choose an appropriate statement from the options given below:
- Both A and R are true and R is the correct explanation of A.
- Both A and R are true and R is not the correct explanation of A.
- A is true but R is false.
- A is false but R is true.
- Both A and R are false.
Write a user-defined function
def Circle()to input radius of a circle from the user. The program calculates area and circumference inside the function. Finally, it returns the values to main program to display the result.Write a user-defined function
def Perfect()to input a number from the user. The program checks whether it is a perfect number or not. Finally, it returns the values to main program to display the result.A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself.
For example, 6 = 1 + 2 + 3
It is a perfect number.