- 1

Hi everyone, how can I do code in recursive with pow while not using its library of cmath

C++

20th May 2022, 7:16 PM
Ali waleed
3 odpowiedzi
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.
20th May 2022, 8:02 PM
OrHy3
OrHy3 - avatar
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; } }
20th May 2022, 9:39 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
0
Thank you very much
20th May 2022, 9:41 PM
Ali waleed