0% found this document useful (0 votes)
4 views3 pages

Recursive Functions in Python

The document contains Python code snippets demonstrating various recursive functions, including calculating factorial, Fibonacci numbers, sum of natural numbers, counting digits, reversing a string, exponential function, sum of digits, finding the greatest common divisor (GCD), and checking for palindromes. Each function is accompanied by driver code that tests its functionality with specific inputs. The examples illustrate the use of recursion in solving common programming problems.

Uploaded by

ptnklbg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Recursive Functions in Python

The document contains Python code snippets demonstrating various recursive functions, including calculating factorial, Fibonacci numbers, sum of natural numbers, counting digits, reversing a string, exponential function, sum of digits, finding the greatest common divisor (GCD), and checking for palindromes. Each function is accompanied by driver code that tests its functionality with specific inputs. The examples illustrate the use of recursion in solving common programming problems.

Uploaded by

ptnklbg
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Factorial calculation by recursion

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

#### driver code #####

num = 10
fact = factorial(num)
print(f'Factorial of {num} is {fact}')

Finding nth Fibonacci number by recursion

def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)

#### driver code #####

num = 25
fibo = fibonacci(num)
print(f'{num}th Fibonacci value is is {fibo}')

Sum of natural numbers by recursion

def sum_natural(n):
if n <= 1:
return n
else:
return n + sum_natural(n - 1)

### driver code ###


num = 100
sum = sum_natural(num)
print(f'Sum of natural numbers from 1 to {num} is {sum}')
Counting digits

def count_digits(n):
if n == 0:
return 0
else:
return 1 + count_digits(n // 10)

### driver code ###


num = 2563214
count = count_digits(num)
print(f'Digits count in {num} is {count}')

Reverse a string

def reverse_string(s):
if len(s) == 0:
return s
else:
return reverse_string(s[1:]) + s[0]

### driver code ###


str1 = "Computer Science"
str2 = reverse_string(str1)
print(f"Reverse of {str1} is {str2}")

Exponential function

def power(x, n):


if n == 0:
return 1
else:
return x * power(x, n - 1)

### driver code ###


base = 5
pwr = 3
result = power(base, pwr)
print(f"{base} to the power of {pwr} is {result}")
Sum of digits

def sum_digits(n):
if n == 0:
return 0
else:
return (n % 10) + sum_digits(n // 10)

### driver code ###


num = 12321
sum = sum_digits(num)
print(f"Sum of all digits in {num} is {sum}")

Greatest Common Divisor - GCD

def gcd(a, b):


if b == 0:
return a
else:
return gcd(b, a % b)

### driver code ###


m = 36
n = 24
result = gcd(m, n)
print(f"GCD of {m}, {n} is {result}")

Palindrome Check

def is_palindrome(s):
if len(s) <= 1:
return True
else:
return s[0] == s[-1] and is_palindrome(s[1:-1])

### driver code ###


str = "malayalam"
if is_palindrome(str):
print(f"{str} is a palindromic word")
else:
print(f"{str} is a non-palindromic world")

You might also like