Python program to discover the intensity of a number using recursion

Discovering the intensity of a number: Here, we are going to execute a python program to discover the intensity of a given number utilizing recursion in Python.

Given the base x and the power y and we need to discover the x to the power y utilizing recursion in Python.

By utilizing recursion – We will duplicate a number (at first with esteem 1) by the number contribution by the client (of which we need to discover the estimation of yth control) for y times.

For duplicating it by y times, we have to call our capacity y times. Since we know the occasions capacity will execute, so we are utilizing for recursion.

Python code to discover the intensity of a number utilizing recursion:

# Python code to find the power of a number using recursion 

# defining the function to find the power
# function accpets base (x) and the power (y)
# and, return x to the power y
def pow(x, y):
    if y == 1:
        return x
    else:
        return pow(x, y-1) * x

# main code
if __name__ == '__main__':
    x = 2 #base
    y = 3  #power
    result = pow(x, y)
    print(x," to the power ", y, " is: ", result)

    x = 10 #base
    y = 3  #power
    result = pow(x, y)
    print(x," to the power ", y, " is: ", result)
    
    x = 12 #base
    y = 5  #power
    result = pow(x, y)
    print(x," to the power ", y, " is: ", result)

Output:

2  to the power  3  is:  8
10  to the power  3  is:  1000
12  to the power  5  is:  248832

Leave a Comment

error: Alert: Content is protected!!