This is a Python Program to find the power of a number using recursion.
The program takes a base and a power and finds the power of the base using recursion.
1. Take the base and exponential value from the user.
2. Pass the numbers as arguments to a recursive function to find the power of the number.
3. Give the base condition that if the exponential power is equal to 1, return the base number.
4. If the exponential power isn’t equal to 1, return the base number multiplied with the power function called recursively with the arguments as the base and power minus 1.
5. Print the final result.
6. Exit.
Here is source code of the Python Program to find the power of a number using recursion. The program output is also shown below.
def power(base,exp): if(exp==1): return(base) if(exp!=1): return(base*power(base,exp-1)) base=int(input("Enter base: ")) exp=int(input("Enter exponential value: ")) print("Result:",power(base,exp))
1. User must enter the base and exponential value.
2. The numbers are passed as arguments to a recursive function to find the power of the number.
3. The base condition is given that if the exponential power is equal to 1, the base number is returned.
4. If the exponential power isn’t equal to 1, the base number multiplied with the power function is called recursively with the arguments as the base and power minus 1.
5. The final result is printed.
Case 1: Enter base: 2 Enter exponential value: 5 Result: 32 Case 2: Enter base: 5 Enter exponential value: 3 Result: 125
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.
- Apply for Programming Internship
- Check Information Technology Books
- Practice Programming MCQs
- Check Python Books
- Apply for Python Internship