juanjo12x

Lab2_Algoritmia_P7_Potencia_Recursive

Apr 13th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int ElevarRecursivo(float x, int n) {
  4.     if(n < 0) {
  5.         return 1; // ElevarRecursivo(x, -n);
  6.     } else if(n == 0) {
  7.         return 1;
  8.     } else {
  9.         if(n == 1) { // Caso base
  10.             return x;
  11.         } else { // Case recursivo
  12.             int aux = ElevarRecursivo(x, n / 2);
  13.             if(n % 2 == 0) { // n es par
  14.                 return aux * aux;
  15.             } else { // n es impar
  16.                 return aux * aux * x;
  17.             }
  18.         }
  19.     }
  20. }
  21. int main(int argc, char** argv) {
  22.     int s=ElevarRecursivo(2,3);
  23.     printf("%d",s);
  24.     return (EXIT_SUCCESS);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment