Posts

Showing posts with the label recursion in python

Recursion in Python

Recursion is an approach to solve a problem which involves a function calling itself either directly or indirectly. Such a function is known as a recursive function. Every recursive method must have a base case which would stop calling the function again and again indefinitely, because the function stack has a limit, which is platform dependent. A Python program to print a string backwards using a recursive function. def reverse(s, n):     if n > 0:         print(s[n], end = '')         reverse(s, n - 1)     elif n == 0:         print(s[0]) s = input('Enter the string: ') reverse(s, len(s) - 1) A Python program to calculate a b using a recursive function. def power(a, b):     if b == 0:         return 1     else:         return a * power(a, b - 1) a = int(input('a = ')) b = int(input('b = ')) p = power(a, b) print('a to the power b: ' + str(p)...