Recursive exponential function java: If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Power of a number :
A number’s power (or exponent) aa represents the number of times xx must be multiplied by itself. It is written as a tiny number above and to the right of the base number.
Recursion:
If you’re familiar with Python functions, you’ll know that it’s typical for one function to call another. It is also feasible for a function in Python to call itself! A recursive function calls itself, and the process of using a recursive function is known as recursion.
Although it may appear strange for a function to call itself, many sorts of programming challenges are better stated recursively.
Given a number N and the power of P. The aim is to develop a Python program that uses recursion to find the power of a number with the given base.
Examples:
Example1:
Input:
Enter some random base =8 Enter some random exponent value = 3
Output:
8 ^ 3 = 512
Example2:
Input:
Enter some random base =17 Enter some random exponent value = 3
Output:
17 ^ 3 = 4913
- Python Program to Reverse a String Using Recursion
- Python Program to Count Number of Digits in a Number using Recursion
- Java Program to Find Nth Power of a Number by Using Recursion
Program to Find the Power of a Number Using Recursion in Python
Below are the ways to find the power of a number using the recursive approach in Python.
1)Using Recursion(Static Input)
Approach:
- Give the exponent as static input and store it in a variable.
- Give the base as static input and store it in another variable.
- To find the power of a number, pass the given exponent and base as arguments to the recursive function.
- Give the base condition in the instance where the exponent argument is 1.
- If the exponent is not equal to 1, return the base multiplied by the function with the parameter’s base and exponent minus 1.
- Until the exponent value is 1, the function calls itself.
- The power of the specified base number should be printed using the print() function.
- Exit of Program
Below is the implementation:
# function which calculates the power of the number recursively def powerRecursion(given_base, given_exp): # Give the base condition in the instance where the exponent argument is 1. if(given_exp == 1): return(given_base) # If the exponent is not equal to 1, return the base multiplied by the function # with the parameter's base and exponent minus 1. if(given_exp != 1): # Until the exponent value is 1, the function calls itself. return(given_base*powerRecursion(given_base, given_exp-1)) # Give the base as static input and store it in variable. given_base = 4 # Enter some random exponent as static input and store it in a variable given_exp = 11 # passing the given base an exponent as arguments to the recursive function powerRecursion print(given_base, "^", given_exp, ' = ', powerRecursion(given_base, given_exp))
Output:
4 ^ 11 = 4194304
2)Using Recursion(User Input)
Approach:
- Give the base as user input using the int(input()) function and store it in a variable.
- Give some exponent as user input using the int(input()) function and store it in a variable
- To find the power of a number, pass the given exponent and base as arguments to the recursive function.
- Give the base condition in the instance where the exponent argument is 1.
- If the exponent is not equal to 1, return the base multiplied by the function with the parameter’s base and exponent minus 1.
- Until the exponent value is 1, the function calls itself.
- The power of the specified base number should be printed using the print() function.
- Exit of Program
Below is the implementation:
# function which calculates the power of the number recursively def powerRecursion(given_base, given_exp): # Give the base condition in the instance where the exponent argument is 1. if(given_exp == 1): return(given_base) # If the exponent is not equal to 1, return the base multiplied by the function # with the parameter's base and exponent minus 1. if(given_exp != 1): # Until the exponent value is 1, the function calls itself. return(given_base*powerRecursion(given_base, given_exp-1)) # Give the base as user input using int(input()) function and store it in a variable. given_base = int(input("Enter some random base =")) # Give some exponent as user input using int(input()) function and store it in a variable given_exp = int(input("Enter some random exponent value = ")) # passing the given base an exponent as arguments to the recursive function powerRecursion print(given_base, "^", given_exp,' = ',powerRecursion(given_base, given_exp))
Output:
Enter some random base =8 Enter some random exponent value = 3 8 ^ 3 = 512
Explanation:
- The base and exponential values must be entered by the user.
- To find the power of a number, the numbers are supplied as arguments to a recursive function.
- The base condition is that the base number is returned if the exponential power is equal to 1.
- If the exponential power is not equal to one, the base number multiplied by the power function is called
- recursively, with the parameters being the base and power minus one.
- The power calculated will be printed.
Related Programs:
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- Python Program to Find the Product of two Numbers Using Recursion
- Python Program to Find if a Number is Prime or Not Prime Using Recursion
- Python Program to Find the Fibonacci Series Using Recursion
- Python Program to Find the Power of a Number Using Recursion
- Python Program to Flatten a Nested List using Recursion
- Python Program to Find the Length of a List Using Recursion