Recursividad 1
This commit is contained in:
1 parent
9a436e8a70
commit
c7f271237e
2 files changed
+46
No files matched your search
File renamed without changes.
@@ -0,0 +1,46 @@
|
||||
import java.util.*;
|
||||
|
||||
public class Recursividad1 {
|
||||
static void main(String[] args) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Introduce dos numeros enteros");
|
||||
int n1 = sc.nextInt();
|
||||
int n2 = sc.nextInt();
|
||||
int resultado = euclides(n1, n2);
|
||||
System.out.printf("El resultado es %d\n", resultado);
|
||||
}
|
||||
// === Metodos recursivos ===
|
||||
// Euclides (mcd)
|
||||
static int euclides(int a, int b){
|
||||
if (a == b) return a;
|
||||
int mayor = Math.max(a, b);
|
||||
int menor = Math.min(a, b);
|
||||
return euclides(mayor - menor, menor);
|
||||
}
|
||||
// Calcular fib(n)
|
||||
static int fib(int n) {
|
||||
if (n <= 1)
|
||||
return n;
|
||||
return fib(n-1) + fib(n-2);
|
||||
}
|
||||
// Maximo comun divisor
|
||||
static int mcd(int a, int b){
|
||||
if(b == 0) return a;
|
||||
return mcd(b, a % b);
|
||||
}
|
||||
// Convertir numero a binario
|
||||
static void bin(int n){
|
||||
if (n < 2) {
|
||||
System.out.print(n);
|
||||
} else {
|
||||
bin(n / 2);
|
||||
System.out.print(n % 2); // Imprimir resto
|
||||
}
|
||||
}
|
||||
// Calcular factorial de un numero
|
||||
static int fac(int n){
|
||||
if (n <= 1)
|
||||
return n;
|
||||
return n * fac(n-1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user