Python program to find power of a number using recursion
View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article Given a number N and power P. The task is to write a Python program to find the power of a number using recursion. Definition: The power of a number can be defined as multiplication of the number repetitively the number of times of its power. Example:
Approach: The idea is to calculate power of a number ‘N’ is to multiply that number ‘P’ times i.e In first example N=2 and P=3, we are getting the result by multiplying 2 three times repetitively which gives us output 8. Below is the implementation: Python
This is a Python Program to find the power of a number using recursion. Problem Description The program takes a base and a power and finds the power of the base using recursion. Problem Solution 1. Take the base and exponential value from the user. Program/Source Code Here is source code of the Python Program to find the power of a number using recursion. The program output is also shown below. Program Explanation 1. User must enter the base and exponential value. Runtime Test Cases 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. Next Steps:
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn. Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses. How do you find the power of a number using recursion?Example: Program to Computer Power Using Recursion
This technique can only calculate power if the exponent is a positive integer. To find power of any number, you can use pow() function. result = pow(base, exponent);
Is power of recursion in Python?The recursive power function, power(base,exponent), must recursively calculate the value of the power and then return it.
How do you find the power of a number in Python?How to find the power of a number in Python. import math. print(math. pow(4,2)) Run. Importing math module in Python.. def power(n,e): res=0. for i in range(e): res *= n. return res. print(pow(4,2)) Run. ... . def power(n, e): if e == 0: return 1. elif e == 1: return n. else: return (n*power(n, e-1)). How do you do recursion exponents in Python?Python Program to Calculate the Power using Recursion. Take the base and exponential value from the user.. Pass the numbers as arguments to a recursive function to find the power of the number.. Give the base condition that if the exponential power is equal to 1, return the base number.. |