You are on page 1of 4

Chapter – 6

Recursion
Assignments with Answers

Q.1 Which of these is false about recursion?


a) Recursive function can be replaced by a non-recursive function
b) Recursive functions usually take more memory space than non-recursive
function
c) Recursive functions run faster than non-recursive function
d) Recursion makes programs easier to understand
Ans. c) Recursive functions run faster than non-recursive function.
Q.2 What are the advantage and disadvantage of recursion?
Ans: Advantages of Recursion:
i. Recursive functions make the code look clean and elegant.
ii. A complex task can be broken down into simpler sub-problems using
recursion.
iii. Sequence generation is easier with recursion than using some nested
iteration.
Disadvantages of Recursion:
i. Sometimes the logic behind recursion is hard to follow through.
ii. Recursive calls are expensive (inefficient) as they take up a lot of memory
and time.
iii. Recursive functions are hard to debug.

Q.3 Which among the following function will be recursive function?


(a) def A( ) { (b) def A():
B() A()
(c) def A( ): (d) def recure( ):
B( ) resure( )
def B( ):
A( )

Ans. (a) not a recursive function.

(b) Yes, it is a recursive function.

(c) Yes, it a recursive function.

(d) No, it is not a recursive function, as they are two different function.
Q.4 Recursion and Iteration are both similar things i.e. anything that we can do with a loop
can be done by recursion. If you, are a programmer which one will be used by you while
making a program.

Ans. As a programmer we will prefer to use both recursion and iteration depending
upon the need. As, Recursion is used by a programmer as :
a) Recursion is a technique to define anything in terms of itself
b) Iteration does not involves the use of additional cost of RAM as compared
to using recursion
c) Recursion is important as it makes the program shorter, and more
simplified.

Q.5 Write a program to print multiplication table of 5 using recursion.


Ans. def table(n,i):
print (n*i)
i=i+1
if i<=10:
table(n,i)
table(5,1)
Q.6 Find the output of following Python codes:
a) def fun(n): b) def fun(x,y):
if n==4: if x==0:
return n return y
else: else:
return 2*fun(n+1) return fun(x-1,x+y)
x = fun(2) a = fun(4,3)
print(x) print(a)

Ans. (a)16 (b) 13


Q. 7 Fill in the line of the following Python code for calculating the factorial of a number.

def fact(n):

if n==1:

return 1

else:

return ---------------

Ans. n * fact(n-1)
Q.8 Write a Python program to print the Fibonacci series using recursion:

Ans. def fibonacci(n):

if n<=1:

return n

else:

return(fibonacci(n-1)+fibonacci(n-2))

num=int(input("How many terms you want to display: "))

for i in range(num):

print(fibonacci(i)," ", end=" ")

Q.9 Python program to find an element in an array using Binary search recursively.

Ans. def binarySearch(list, beg, end, x):


while beg<= end:
mid = beg + (end - beg)//2;
# Check if x is present at mid
if list[mid] == x:
return mid
# If x is greater, ignore left half
elif list[mid] < x:
beg = mid + 1
# If x is smaller, ignore right half
else:
end = mid - 1
return -1
#driver code
list = [ 2, 3, 4, 10, 40 ]
x = 10
position = binarySearch(list, 0, len(list)-1, x)
if position != -1:
print( “Element is present at index” , position)
else:
print (“Element is not present in list”)
Q.10 Write a Python program to find ab using recursion.
Ans. def power(a,b):
if(b= =0):
return 1
else:
return a*power(a,b-1)
n=int(input("enter the number: "))
p=int(input("enter the power: "))
print("Power of ",n,"to",p,"is:",power(n,p))

You might also like