Recursive Power
Completed
Write a recursive double power(double b, int e) using b^e = b^(e/2) squared, and print the result with 4 decimals. Negative exponents mean 1 / b^(-e).
Input
One line: b e.
Output
One line.
Example
Input
2 10
Output
1024.0000
Editor
C
Hints
Compute the half power once and reuse it.
Solution explanation
Halving the exponent means only about log₂(e) calls.
Show solution
#include <stdio.h>
double power(double b, int e) {
if (e == 0) return 1;
if (e < 0) return 1 / power(b, -e);
double half = power(b, e / 2);
if (e % 2 == 0) return half * half;
return half * half * b;
}
int main() {
double b;
int e;
scanf("%lf %d", &b, &e);
printf("%.4f\n", power(b, e));
return 0;
}