Recursion
Recursion
9 Define a recursive function SUM1TON(n) to calculate sum of all the number from 1 to n
An def sum(n): if n==1:
s return 1 else:
return n+sum(n-1)
10 Define a recursive function FIBO(n) to generate Fibonacci series for first n numbers For e.g. if n is
6, the numbers to generate are 0 1 1 2 3 5
An def fibo(n):
s if n<=1:
return n
else: return fibo(n-1)+fibo(n-2)
sum =
Alter(20)
print(sum)
An 113
s
13 Find the output of following Python code:
def A(n):
if n==1:
return 1
else:
return n + B(n-1)
def B(n):
if n==1:
return 5
else:
return n + A(n-1)
val =
A(10)
print(val)
An 59
s
14 Find the output of following Python code:
def fun(x):
if(x > 0):
x -= 1
fun(x)
print(x , end=" ")
x -= 1
fun(x)
a=4
fun(
a)
An 0120301
s
15 Find the output of following Python code:
def fun(n):
if n==4:
return n
else:
return 2*fun(n+1)
x = fun(2)
print(x)
An 16
s
16 Find the output of following Python code:
def fun(x,y):
if x==0:
return y
else:
return fun(x-1,x+y)
a=
fun(4,3)
print(a)
An 13
s
17 Find the output of following Python code:
def fun(n):
if n==0:
return print(n
%2,end='') fun(n//2)
fun(25)
An 10011
s
18 Which of these is not true about recursion?
a) Making the code look clean
b) A complex task can be broken into sub-problems
c) Recursive calls take up less memory
d) Sequence generation is easier than a nested iteration
An c)
s
19 Which of these is not true about recursion?
a) It‟s easier to code some real-world problems using recursion than non-recursive equivalent
b) Recursive functions are easy to debug
c) Recursive calls take up a lot of memory
d) Programs using recursion take longer time than their non-recursive equivalent
An b)
s
20 Find the output of the following Python code:
def fun1(msg,length):
if length==0:
return
else:
print(msg[length-1],end='')
fun1(msg,length-1)
fun1('amit',4)
An tima
s