Challenge: Recursive Powers

Write a recursive function power(x, n) that returns the value of xnx^n (assume that nn is an integer). Here are the 44 following cases that you need to handle.

1. Base Case

Start by writing the base case x0=1x^0 = 1 for any value of xx.

2. Recursive case: nn is odd

In this step, write the recursive case for which nn is odd. Assume you have a function isOdd() to check if nn is odd.

3. Recursive case: nn is even

In this step, write the recursive case for which nn is even. Assume you have a function isEven() to check if nn is even.

4. Recursive case: nn is negative

In this step, write the recursive case for which nn is negative. Compute xx raised to n-n recursively, and return the reciprocal of that number.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy