Recursion Butad John Esteve
Recursion Butad John Esteve
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 check_armstrong(num):
return num == armstrong(num, len(str(num)))
# 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")
# 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.")
# Invalid input
else:
print("Invalid choice, please select between 0 and 6.")
OUTPUT