0% found this document useful (0 votes)
18 views4 pages

Recursion Butad John Esteve

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)
18 views4 pages

Recursion Butad John Esteve

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
You are on page 1/ 4

# 1.

Palindrome Number
def is_palindrome(num, rev=0):
if num == 0:
return rev
else:
rev = (rev * 10) + (num % 10)
return is_palindrome(num // 10, rev)

def check_palindrome(num):
return num == is_palindrome(num)

# 2. Armstrong Number
def power(num, exp):
if exp == 0:
return 1
return num * power(num, exp - 1)

def armstrong(num, length):


if num == 0:
return 0
digit = num % 10
return power(digit, length) + armstrong(num // 10, length)

def check_armstrong(num):
return num == armstrong(num, len(str(num)))

# 3. Reverse Given Number


def reverse_num(num, rev=0):
if num == 0:
return rev
rev = (rev * 10) + (num % 10)
return reverse_num(num // 10, rev)

# 4. Prime Numbers between two integers


def is_prime(n, divisor=None):
if divisor is None:
divisor = n - 1
if divisor == 1:
return True
if n % divisor == 0:
return False
return is_prime(n, divisor - 1)

def find_primes(start, end):


if start > end:
return []
if start > 1 and is_prime(start):
return [start] + find_primes(start + 1, end)
return find_primes(start + 1, end)

# 5. Finding the Leap Year


def is_leap_year(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
return False
return True
return False

# 6. Fibonacci Series
def fibonacci(n, a=0, b=1, result=None):
if result is None:
result = []
if n == 0:
return result
result.append(a)
return fibonacci(n - 1, b, a + b, result)

# Main loop to accept user input and proceed to the selected recursive function
while True:
print("\nSelect the operation you want to perform:")
print("1: Palindrome Number")
print("2: Armstrong Number")
print("3: Reverse Given Number")
print("4: Prime Numbers between two integers")
print("5: Finding the Leap Year")
print("6: Fibonacci Series")
print("0: Exit")

choice = int(input("\nEnter choice (0-6): "))

# Exit option
if choice == 0:
print("Exiting...")
break

# 1. Palindrome Number
if choice == 1:
num = int(input("Enter a number to check if it is a palindrome: "))
if check_palindrome(num):
print(f"{num} is a palindrome.")
else:
print(f"{num} is not a palindrome.")

# 2. Armstrong Number
elif choice == 2:
num = int(input("Enter a number to check if it is an Armstrong number: "))
if check_armstrong(num):
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")

# 3. Reverse Given Number


elif choice == 3:
num = int(input("Enter a number to reverse: "))
print(f"Reverse of {num} is {reverse_num(num)}")

# 4. Prime Numbers between two integers


elif choice == 4:
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
print(f"Prime numbers between {start} and {end}: {find_primes(start, end)}")

# 5. Finding the Leap Year


elif choice == 5:
year = int(input("Enter a year to check if it is a leap year: "))
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
# 6. Fibonacci Series
elif choice == 6:
n = int(input("Enter the number of terms for the Fibonacci series: "))
print(f"Fibonacci series up to {n} terms: {fibonacci(n)}")

# Invalid input
else:
print("Invalid choice, please select between 0 and 6.")

OUTPUT

You might also like