CBSE Class 11 Computer Science Question 19 of 22

Introduction to Python Modules — Question 19

Back to all questions
19
Question

Question 19

Define a function which accepts n as an argument and prints Fibonacci series till n.

Solution
def print_fibonacci(n):
    a, b = 0, 1  
    while a <= n:
        print(a, end=' ')
        a, b = b, a + b  

n = int(input("Enter a number: "))
print_fibonacci(n)
Output
Enter a number: 10
0 1 1 2 3 5 8
Answer

def
print_fibonacci
(
n
):
a
,
b
=
0
,
1
while
a
<=
n
:
print
(
a
,
end
=
' '
)
a
,
b
=
b
,
a
+
b
n
=
int
(
input
(
"Enter a number: "
))
print_fibonacci
(
n
)
Output
Enter a number: 10
0 1 1 2 3 5 8