Python-Power of a Number By Recursion

Write a program to compute the power of a number by recursion

# Power of a number by recursion
# Write recursive mathematical definition for computing x^n for a positive integer n
# and a positive integer x.
# E.g. Computer x ^ n by recursion

def power(x, n):
    if (n == 0):  #base case
        return 1
    elif n == 1:
        return x
        return (x * power(x, n - 1))

x = int(input("Enter the number x:"))

n = int(input("Enter the power of x:"))

#take care of mischief by user
if (x < 0):
    x = -x

if (n < 0):
    n = -n

print(x, " raised to ", n, " is ",power(x, n))
%d bloggers like this: