Write A Python Program To Calculate The Value Of A To The Power B Using Recursion

Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Program To Calculate The Value Of 'A' To The Power 'B' Using Recursion

 

Title : 

Write A Python Program To Calculate The Value Of A To The Power B Using Recursion


Write A Python Program To Calculate The Value Of A To The Power B Using Recursion



 

Description : 

Hi guys, in this article you will learn a Recursive function to calculate the value of a raise to power of b. Given the value of a as floating point number, and value of b as an integer we will be defining a recursive function to calculate the power of b. Define a function named as power by passing to variables a and b, use the If statement to check if value of b = 0 then return 1. If the value of b is negative, convert the value of b into positive and return the floating point value 1 divided by the power of a and b if the value of b is greater than zero multiply the value of a with the recursive function by calling power of a and b reducing the value of b by 1 using -1


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


    


Use the input function call to ask a user to enter Value of A and B story into two variables named as base and exponent Now call the recursive function named as power by passing base and exponent and store the return value of the recursive function into a result variable use the print function call to print the result of a raise to power of b


    
base = float(input("Enter the value of a: "))
exponent = int(input("Enter the value of b: "))

result = power(base, exponent)
print(f"{base} raised to the power of {exponent} is: {result}")



    




Complete Python Code : 


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

base = float(input("Enter the value of a: "))
exponent = int(input("Enter the value of b: "))

result = power(base, exponent)
print(f"{base} raised to the power of {exponent} is: {result}")



    

 

 

Output : 


    
Enter the value of a: 2
Enter the value of b: 24
2.0 raised to the power of 24 is: 16777216.0

 

Output : 


    
Enter the value of a: 15
Enter the value of b: -2
15.0 raised to the power of -2 is: 0.0044444444444444444



Conclusion :

The above Python program uses a Recursive function to calculate the power of a and b by checking the three conditions, value of B is equal to 0, Value of b is less than 0 and value of b is greater than zero. The power is calculated appropriately for all the about three conditions by multiplying the value of a with the remaining recursive functions. In the main program it uses input function calls to ask the user to enter the value of a and b and then calls a function and prints the result. Comment it down below if you have any suggestions to improve the above Python program

 

Post a Comment

0 Comments