basics · beginner · ~15 min

Integer power recursively

Use the exponent's recurrence b^e = b * b^(e-1).

Challenge

Raise an integer to a non-negative power using recursion instead of a loop.

Task

Implement long int_power(int base, int exp) that returns base raised to the power exp. Use recursion via the recurrence base^exp = base * base^(exp-1), with base^0 == 1 as the base case. No main — the grader calls it.

Input

An int base and a non-negative int exp.

Output

Returns base^exp as a long.

Example

int_power(2, 10)   ->   1024
int_power(3, 0)    ->   1
int_power(5, 1)    ->   5

Edge cases

  • exp == 0 returns 1 for any base.

Rules

  • Must be recursive.

Input format

An int base and a non-negative int exp.

Output format

base raised to exp, as a long.

Constraints

Must be recursive; base^0 == 1.

Starter code

long int_power(int base, int exp) {
    /* TODO */
    return 1;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.