Power of a Number

Given two integers base and exponent, return base raised to the power exponent using recursion.

When exponent is 0, the result is 1.

Note:

A normal iterative or built-in power solution will pass the test cases, but the goal of this problem is to solve it using recursion and strengthen your understanding of recursive calls and base cases.

Example 1:

Input: base = 2, exponent = 5

Output: 32

Explanation: 2^5 = 2 * 2 * 2 * 2 * 2 = 32.

Example 2:

Input: base = 7, exponent = 0

Output: 1

Explanation: Any nonzero number raised to the power 0 is 1.

Now Your Turn!

Pick the correct output for the given input

Input: base = 3, exponent = 4

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= base <= 10
  • 0 <= exponent <= 9

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int power(int base, int exponent) {
// Your code goes here
}
};
Test Case

Input:

Base
Exponent