Home » Python recursion in calculation of power

Python recursion in calculation of power

Power a to b using recursion

def power(a,b):
    if b==0:
        return 1
    else:
        return a*power(a,b-1)

# __main__
print("Enter only the positive numbers below")
num=int(input("Enter base number:"))
p=int(input("raised to the power of:"))
result=power(num,p)
print(num,"raised to the power of",p,"is",result)

Contents

Output:

Enter only the positive numbers below
Enter base number:7
raised to the power of:3
7 raised to the power of 3 is 343