The Open Source Swiss Army Knife

/code/c/algorithms_c/
/code/c/algorithms_c/ + sub-categories
http://www.sirfsup.com/
web directory content
    
      

Not logged in
Chat Register Login
return to:  http:/www.sirfsup.com      /code   /c   /algorithms_c 
Permalink: exponentiate.cpp
Title: need compile with g-plus-plus
article options : please login   |  raw source view  

#include <stdio.h>

int Power(int x, int n) {
  if (n == 0) return 1;
  if (n == 1) return x;
  int quotient = n%2;
  if (quotient != 0)   // is odd
     return Power(x, n-1) * x;  
     else return Power(x*x, n/2); // even 
}

main() {
  int x = 4;
  int n = 4 ;
    printf("x^n = %d\n", Power(x, n));

  return 0;
}

Leave a Reply
Your Name:     anonymous
Your Email:
Website:  
Comments:

The author will be notified of your reply.
return to top