On this page we will see how to find Power of a Number in Python. We will see three methods to do so. User have to give base and power as input. Power of a number is basically multiplying the base number by itself , power number of time.
Example :
Input : base = 2, power = 3
Output : 8
Explanation : 23 means 2 is base and 3 is power which is 3 times 2 ( 2 x 2 x 2 ) which is equals to 8
Method 1 : Using Recursion
Algorithm
We will first assign value of a as base and b as power of number.
We will pass a and b to function power.
We will use recursion to find the answer.
We will recursively call the function power and each time we will return a*power(a,b-1) till the time b is not equal to zero. Each recursion call will return a multiply by a till b is not zero. In each call we will decrement the value of b by 1.
If b is equal to zero then we will return from the function.
Login/Signup to comment