python recursion notes class 12
Recursion chapter Revision Notes
Recursion in Python complete Chapter
IT is a method function calling where a function can be called ( INVOKE) where a function can be called within its definition.
A recursive function has to fulfil an important condition to be used in a program: it has to terminate. A recursive function terminates, if with every recursive call the solution of the problem is downsized and moves towards a base case. A base case is a case, where the problem can be solved without further recursion. A recursion can end up in an infinite loop, if the base case is not met in the calls.
4! = 4 * 3!
3! = 3 * 2!
Replacing the calculated values gives us the following expression
4! = 4 * 3 * 2 * 1
In other words, recursion in computer science is a method where the solution to a problem is based on solving smaller instances of the same problem.
Q1. Write a programme using Recursion to calculate factorial of a number
Ans. def fact(n):
Q2. using recursion to calculate sum of n numbers.
ans. def sum(n):
return(n + sum(n-1))
Q3. write a programme using recursion to calculate and display Fibonacci series.
ans. def fibonacci(x):
fibonacci(x-1) + fibonacci(x-2):
Q4. recursive function to print string in reverse order.
ans. def reverse(s):
return(s[-1] + reverse(s[0:len(s)-1))
Q5. recursive programme to find power of one number raised to another.
ans. def power(x,y):
for x in range (1,Y+1):
print(“power of “,a,”raised to “,b,”=”,c)
Q6. programme to count no. of T in file story.txt .
for line in f:
print(” No. of T present=”,count)
Q7. to find no. of lines starting with T in story.txt file.
for line in f:
print(” no. of line starting with T=”,count)
Q8. write recursive function to print table of 5.
ans. def printcount(s,n):