- 1
Hi everyone, how can I do code in recursive with pow while not using its library of cmath
C++
3 Respuestas
0
Did you mean "how to do pow() function in recursive way?". If yes, and you meant for integers, I would do like this:
int pow(int base, int e) {
if (e == 0)
return 1;
return base * pow(base, e - 1);
}
Note that this only works when exponent is positive, otherwise the function will run forever.
0
My solution
int pow(int b, int e) {
if( e == 0 ) return 1;
else {
int c = (e & 1 ? b : 1);
int x = pow(b, e>>1);
return c * x * x;
}
}
0
Thank you very much