fork(2) download
  1. #include <stdio.h>
  2.  
  3. // Naive recursive solution to calculate pow(x, n)
  4. // using Divide & Conquer
  5. int power(int x, unsigned n)
  6. {
  7. // base condition
  8. if (n == 0)
  9. return 1;
  10.  
  11. if (n & 1) // if n is odd
  12. return x * power(x, n / 2) * power(x, n / 2);
  13.  
  14. // else n is even
  15. return power(x, n / 2) * power(x, n / 2);
  16. }
  17.  
  18. // main function
  19. int main(void)
  20. {
  21. int x = -2;
  22. unsigned n = 10;
  23.  
  24. printf("pow(%d,%d) = %d", x, n, power(x, n));
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 4528KB
stdin
Standard input is empty
stdout
pow(-2,10) = 1024