Ejemplos de Recursividad

Descargar como docx, pdf o txt
Descargar como docx, pdf o txt
Está en la página 1de 4

Ejemplos de Recursividad

1. Planteamiento Ejercicio 1. Programar un algoritmo recursivo que calcule el factorial de un


número.
Solución:
view plainprint?

1. int factorial(int n){  
2. if(n==0){  
3. return 1; //Caso Base  
4. }  
5. else {  
6. return n * factorial(n-1);  //Fórmula Recursiva  
7. }  
8. }  

2. Planteamiento Ejercicio 2: Programar un algoritmo recursivo que calcule un número de la


serie fibonacci.
Solución:
view plainprint?

1. int fibonaci(int n){  
2. if(n==1 || n==2) {  
3. return 1;  
4. }  
5. else{  
6. return fibonaci(n-1)+fibonaci(n-2);  
7. }  
8. }  

3. Planteamiento Ejercicio 3: Programar un algoritmo recursivo que permita hacer la división


por restas sucesivas. ver mas...
Solución:
view plainprint?

1. int division (int a, int b) {  
2. if(b > a) {  
3. return 0;  
4. }  
5. else {  
6. return division(a-b, b) + 1;  
7. }  
8. }  

4. Planteamiento Ejercicio 4: Programar un algoritmo recursivo que permita invertir un


número.Ejemplo: Entrada:123  Salida:321
Solución:
view plainprint?

1. int invertir (int n) {  
2.  if (n < 10) {         //caso base  
3.   return n;  
4.  }  
5.  else {  
6.   return (n % 10) + invertir (n / 10) * 10;  
7.  }  
8. }  
5. Planteamiento Ejercicio 5: Programar un algoritmo recursivo que permita sumar los dígitos
de un número.Ejemplo: Entrada:123  Resultado:6
Solución:
view plainprint?

1. int sumar_dig (int n) {  
2. if (n == 0) {      //caso base  
3. return n;  
4. }  
5. else {  
6. return sumar_dig (n / 10) + (n % 10);  
7. }  
8. }  

6. Planteamiento Ejercicio 6: Programar un algoritmo recursivo que permita hacer una multiplicación,


utilizando el método Ruso. Para mas información: aquí.
Solución:
view plainprint?

1. int mult_rusa(int A, int B) {  
2. if(A==1){  
3. return (B);  
4. }  
5. if(A%2!=0){  
6. return (B+mult_rusa( A/2 , B*2));  
7. }  
8. else{  
9. return(mult_rusa( A/2 , B*2));  
10. }                        
11. }  

7. Planteamiento Ejercicio 7: Programar un algoritmo recursivo que permita sumar los


elementos de un vector.
Solución:
view plainprint?

1. int suma_vec(int v [], int n) {  
2. if (n == 0) {  
3. return v [n];  
4. }  
5. else {  
6. return suma_vec(v, n - 1) + v [n];  
7. }  
8. }   

8. Planteamiento Ejercicio 8: Programar un algoritmo recursivo que permita multiplicar los


elementos de un vector.
Solución:
view plainprint?

1. int multiplicar (int vec [], int tam) {  
2. if (tam == 0) {  
3. return (vec [0]);  
4. }  
5. return (vec [tam] * multiplicar (vec, tam - 1));  
6. }  

9. Planteamiento Ejercicio 9: Programar un algoritmo recursivo que calcule el Maximo comun


divisor de dos números.
Solución:
view plainprint?

1. int sacar_mcd(int a, int b) {  
2. if(b==0) {  
3.     return a;  
4. }  
5. else {  
6.     return sacar_mcd(b, a % b);  
7. }  
8. }  

10. Planteamiento Ejercicio 10: Programar un algoritmo recursivo que determine si un número


es positivo/negativo.
Solución:
view plainprint?

1. public boolean positivo(int n){  
2.    if(n<0) return true;  
3.    else return negativo(n);  
4.   }  
5.   
6.   public boolean negativo(int n){  
7.    if(n>0) return false;  
8.    else return  positivo(n);  
9.   }  
11. Planteamiento Ejercicio 11: rogramar un algoritmo recursivo que determine si un número
es impar utilizando recursividad cruzada.
Solución:
view plainprint?

1. public boolean par(int n){  
2. if(n==0) {  
3. return true;  
4. }  
5. else {  
6. return impar(n-1);  
7. }  
8. }  
9.   
10. public boolean impar(int n){  
11. if(n==0) {  
12. return false;  
13. }  
14. else {  
15. return par(n-1);  
16. }  
17. }   
12. Planteamiento Ejercicio 12: Programar un algoritmo recursivo que permita sumar los
elementos de una matriz.
Solución:
view plainprint?

1.  int suma (int fila, int col, int orden, int mat [] [])  
2.   {  
3. if (fila == 0 && col == 0)  
4.   return mat [0] [0];  
5. else  
6.   if (col < 0)  
7. return suma (fila - 1, orden, orden, mat);  
8.   else  
9. return mat [fila] [col] + suma (fila, col - 1, orden, mat);  
10.   }  
13. Planteamiento Ejercicio 13: Programar un algoritmo recursivo que muestre el numero
menor de un vector.
Solución:
view plainprint?

1. int menorvec (int x [], int n, int menor) {  
2. if (n == 0) {  
3. if (menor > x [n]) {  
4.  return x [0];  
5. }  
6.      else {  
7.  return menor;  
8. }  
9. }  
10.  else{  
11. if (menor > x [n]) {  
12.  return menorvec (x, n - 1, x [n]);  
13. }  
14.      else {  
15.  return menorvec (x, n - 1, menor);  
16. }  
17. }  
18. }  
19.   
20. int mayorvec (int numeros [], int posicion) {  
21.     int aux;  
22.     if (posicion == 0) {  
23.  return numeros [posicion];  
24.   }  
25.     else {  
26.  aux = mayor (numeros, posicion - 1);  
27.  if (numeros [posicion] > aux){  
28.   return numeros [posicion];  
29.  }  
30.  else{  
31.   return mayor (numeros, posicion - 1);  
32.  }  
33.      }   
34. } 

También podría gustarte

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy