Integer Power Function (Recursive)

Easy ⏱ 8 min 81% acceptance ★★★★★ 4.5
Implement power(base, exp) computing base exp</code> for a non-negative integer <code>exp</code>, using recursion — no <code> operator, no loops.

Examples

Example 1
Input
power(2, 10)
Output
1024
Explanation

2 multiplied by itself 10 times.

Example 2
Input
power(5, 0)
Output
1
Explanation

Any base to the power 0 is 1 — the base case.

Constraints

  • exp >= 0
  • Do not use the ** operator.

Topics

RecursionMath

Companies

MicrosoftHCLTech

Hints

Hint 1

Base case: exp == 0, return 1.

Hint 2

Recursive case: base * power(base, exp - 1).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.