Created
July 26, 2016 15:05
-
-
Save victorhtorres/7f1068e813a290937a385e5d47d58b6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
/** | |
* | |
* @author vtorr_000 | |
*/ | |
public class Main { | |
// Hallar la potencia de forma recursiva por pila | |
public static int potenciaRecursivaPila(int b, int e) { | |
// caso base | |
if (e == 0) { | |
return 1; | |
} else { | |
// caso general | |
return potenciaRecursivaPila(b, --e) * b; | |
} | |
} | |
// Hallar la potencia de forma recursiva por cola | |
public static int potenciaRecursivaCola(int b, int e) { | |
int acum = 1; | |
return potenciaRecursivaCola(b, e, acum); | |
} | |
private static int potenciaRecursivaCola(int b, int e, int acum) { | |
//caso base | |
if (e == 0) { | |
return acum; | |
} else { | |
//caso general | |
return potenciaRecursivaCola(b, --e, b * acum); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
System.out.print("Digite base entero positivo:"); | |
int inputBase = scanner.nextInt(); | |
System.out.print("Digite exponente entero positivo:"); | |
int inputExpo = scanner.nextInt(); | |
// Imprimir resultados | |
System.out.printf("Potencia recursiva en pila:\n%d%s%d %s %d\n" | |
+ "Potencia Recursiva en cola:\n%d%s%d %s %d\n", | |
inputBase, "^", inputExpo, "=", potenciaRecursivaPila(inputBase, inputExpo), | |
inputBase, "^", inputExpo, "=", potenciaRecursivaCola(inputBase, inputExpo)); | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment