Skip to content
Related Articles

Related Articles

Improve Article

Python program to find the power of a number using recursion

  • Last Updated : 06 May, 2021

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

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 power is 0 then return 1
    if P == 0:
        return 1
      
    # if power is 1 then number is
    # returned
    elif P == 1:
        return N
      
    else:
        return (N*power(N, P-1))
  
# Driver program
N = 5
P = 2
  
print(power(N, P))

Output:

25

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :