Hi, I wrote this function to calculate powers, like x^n. It works for integer values, but I can't get it to calculate roots, like root 5 = 5^(1/2).
Can someone tweek this a little bit for me? thx in advance.
(The base is x, the exponent is n)
int power(int x, int n)
{
int r; // result of calculation
int p;
r = 1;
p = x;
while (n > 0)
{
if (n % 2) // n is even
r = r * p;
p = p * p;
n = n / 2;
}
return r;
}