View Discussion
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:
Input: N=2 , P=3
Output: 8
Input: N=5 , P=2
Output: 25
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
def
power[N, P]:
if
P
=
=
0
:
return
1
return
[N
*
power[N, P
-
1
]]
N
=
5
P
=
0
print
[power[N, P]]
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.
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.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]]
Program Explanation
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.
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:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
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.