I am trying to get it to do something like this, if it is (x^10) it will do (x^5)*(x^5), and if x^11 it will do (x^5)*(x^5)*x.Code:

#include <stdio.h>

#define BAD -1

int Power(int x, int n);

main()

{

Power(3,20);

}

int Power(int x, int n)

{

int middle;

if(n < 0) exit(BAD);

else if(n == 1) return x;

else if(n == 0) return 1;

else if( (n % 2) == 0) // even

{

middle = n/2;

printf("In even middle = %d\n", middle);

return Power(x, middle) * Power(x, middle);

}

else

if( (n % 2) == 1) //odd

{

middle = n/2;

printf("In odd middle = %d\n", middle);

return Power(x, middle) * Power(x, middle) * x;

}

}

It works for Power(3, (0->4)), but once I make the power higher than 4 or 5 it will give me the incorrect answer. I dont know why it works up to a limit of numbers, and then starts to give me wrong answers.

Thanks.