First of all, you need to move the lines that take input outside of Power(), or else you'll find yourself taking input over and over again when the function recurses.
Second, you need to actually recurse by having the function call itself. Say, by doing Power(x, n/2). Notice that instead of doing
1 2 3 4
|
if (n%2==0)
result = Power(x, n/2) * Power(x, n/2);
else
result = Power(x, n/2) * Power(x, n/2 + 1);
|
it's preferable to do
1 2 3 4 5 6 7
|
if (n%2==0){
result = Power(x, n/2);
result = result * result;
}else{
result = Power(x, n/2);
result = result * result * x;
}
|
It's always best to avoid consecutive recursive calls if at all possible.
Another thing is that 1%2==1, so the condition on line 22 will never be evaluated. You should check for the base case of a recursive function as soon as possible:
1 2 3 4 5
|
double Power(float X, int N){
if (N==1)
return X;
//...
}
|
Generally speaking, it's always best to check for exceptional or degenerate cases early, so that you can concentrate on the general cases in the bulk of the code.
For example, if you have code that draws a circle, you want to check if the radius is 0 as soon as possible so that you can call the point drawing routine and let the circle drawing routine focus on drawing well-behaved circles.