24.Example programs using Recursion
1.Print the factorial of a number using recursion
def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
n=int(input('Enter n..:'))
x=fact(n)
print("factorial of ..",n ," ..is.. ",x)
2.Program to print n’th Fibonacci number
3.Program to find sum of the digits of a number
4.Program to convert decimal number to binary using recursion
5.Program to find sum of n natural numbers using recursion
6.Program to find nPr using a recursive factorial function
# Base case: if one of the numbers is zero
if b == 0:
return a
# Recursive case: increment a and decrement b
return add_positive_numbers(a + 1, b - 1)
# Input two positive numbers
num1 = int(input("Enter the first positive number: "))
num2 = int(input("Enter the second positive number: "))
# Ensure both numbers are positive
if num1 < 0 or num2 < 0:
print("Both numbers must be positive.")
else:
# Calculate the sum using recursion
result = add_positive_numbers(num1, num2)
# Print the result
print(f"The sum of {num1} and {num2} is {result}")
9.Recursive function to multiply two positive numbers.
# Recursive function to multiply two numbers
def multiply(a, b):
# Base case: if one of the numbers is zero
if b == 0:
return 0
# Recursive case: multiply by adding a to the result of multiplying a with (b - 1)
return a + multiply(a, b - 1)
# Input two numbers
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
# Calculate the product using recursion
result = multiply(num1, num2)
# Print the result
print(f"The product of {num1} and {num2} is {result}")
def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
n=int(input('Enter n..:'))
x=fact(n)
print("factorial of ..",n ," ..is.. ",x)
output:
Enter n..:5
factorial of .. 5 ..is....120
def fib(n):
if n <= 1:
return n
else:
return fib(n-1) + fib(n-2)
n=int(input('Enter n…'))
x=fib(n)
print (n,"th Fibonacci number is...",x)
output:
Enter n…5
5 th Fibonacci number is... 5
def sumd(n):
if n == 0:
return 0
else:
return n%10 + sumd(n//10)
n=int(input('Enter n'))
s=sumd(n)
print ("sum of the digits of ..",n ," ..is.. ",s)
if n == 0:
return 0
else:
return n%10 + sumd(n//10)
n=int(input('Enter n'))
s=sumd(n)
print ("sum of the digits of ..",n ," ..is.. ",s)
output:
Enter n...123
sum of the digits of .. 123 ..is.. 6
def decb(n):
if n==0:
return 0
else:
return n%2 + 10* decb(n//2)
n=int(input('Enter n.:'))
b=decb(n)
print("The binary equivalent of ..",n ," ..is.. ",b)
if n==0:
return 0
else:
return n%2 + 10* decb(n//2)
n=int(input('Enter n.:'))
b=decb(n)
print("The binary equivalent of ..",n ," ..is.. ",b)
output
Enter n.:12
The binary equivalent of .. 12 ..is.. 1100
def sum(n):
if n ==0:
return 0
else:
return n + sum(n-1)
n=int(input('Enter n..'))
s=sum(n)
print("The sum of natural numbers up to ...",n ," ..is.. ",s)
if n ==0:
return 0
else:
return n + sum(n-1)
n=int(input('Enter n..'))
s=sum(n)
print("The sum of natural numbers up to ...",n ," ..is.. ",s)
output
Enter n..10
The sum of natural numbers up to ... 10 ..is.. 55
def fact(n):
if n==0:
return 1
else:
return n*fact(n-1)
n=int(input('Enter n..'))
r=int(input('Enter r..'))
print("nPr=", fact(n)//fact(n-r))
if n==0:
return 1
else:
return n*fact(n-1)
n=int(input('Enter n..'))
r=int(input('Enter r..'))
print("nPr=", fact(n)//fact(n-r))
output
Enter n..5
Enter r..3
nPr= 60
7.GCD of two numbers using recursion ( university question)
def gcd_recursive(a, b):
if b == 0:
return a
else:
return gcd_recursive(b, a % b)
x=int(input('Enter x='))
y=int(input('Enter y='))
gcd=gcd_recursive(x,y)
print(f'gcd of {x} and {y} is={gcd}')
8.Recursive Function to add two positive numbers.
def add_positive_numbers(a, b):# Base case: if one of the numbers is zero
if b == 0:
return a
# Recursive case: increment a and decrement b
return add_positive_numbers(a + 1, b - 1)
# Input two positive numbers
num1 = int(input("Enter the first positive number: "))
num2 = int(input("Enter the second positive number: "))
# Ensure both numbers are positive
if num1 < 0 or num2 < 0:
print("Both numbers must be positive.")
else:
# Calculate the sum using recursion
result = add_positive_numbers(num1, num2)
# Print the result
print(f"The sum of {num1} and {num2} is {result}")
# Recursive function to multiply two numbers
def multiply(a, b):
# Base case: if one of the numbers is zero
if b == 0:
return 0
# Recursive case: multiply by adding a to the result of multiplying a with (b - 1)
return a + multiply(a, b - 1)
# Input two numbers
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
# Calculate the product using recursion
result = multiply(num1, num2)
# Print the result
print(f"The product of {num1} and {num2} is {result}")
Comments
Post a Comment