Skip to content

Instantly share code, notes, and snippets.

@jofese
Created November 9, 2014 21:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jofese/4fe1663b37deff9623c7 to your computer and use it in GitHub Desktop.
Save jofese/4fe1663b37deff9623c7 to your computer and use it in GitHub Desktop.
Ejercicios Implementados en c++ sobre Recursividad
/* Autor: Joel Cesar Fernandez Segura
Fecha: 28/08/2014
Tema: Recursividad
Ejercicio 3: convertir numero decimal a binario
IDE: CodeBlocks
Pagina Web: http://codebotic.blogspot.com
*/
#include<iostream>
#include<cstdlib>
using namespace std;
int binario(int n){
if(n>1) binario(n/2);
cout<<n%2;
}
int main( void ){
system("color 0a");
int nro;
cout<<"\n\t\t[ RECURSIVIDAD ]\n";
cout<<"\t\t------------------------\n\n";
cout<<" EJERCICIO 2: Convertir a binario un numero decimal "<<endl<<endl;
do{
cout<<" INGRESE NUMERO: ";
cin>>nro;
if(nro<0) cout<<"\nINGRESE UN NUMERO ENTERO Y POSITIVO... \n";
}while(nro<0);
cout<<endl;
cout<<"\n Numero:"<<nro<<endl;
cout<<"\n Binario:";
binario(nro);
return 0;
}
/* Autor: Joel Cesar Fernandez Segura
Fecha: 28/08/2014
Tema: Recursividad
Ejercicio 4: Invertir un numero
IDE: CodeBlocks
Pagina Web: http://codebotic.blogspot.com
*/
#include<iostream>
#include<cstdlib>
using namespace std;
void invertir(int nro){
cout<<nro%10;
if (nro>10) invertir(nro/10);
}
int main( void ){
system("color 0a");
int nro;
cout<<"\n\t\t[ RECURSIVIDAD ]\n";
cout<<"\t\t------------------------\n\n";
cout<<" EJERCICIO 4: Invertir un numero "<<endl<<endl;
do{
cout<<" INGRESE NUMERO: ";
cin>>nro;
if(nro<0) cout<<"\nINGRESE UN NUMERO ENTERO Y POSITIVO... \n";
}while(nro<0);
cout<<"\n NUMERO:"<<nro;
cout<<"\nINVERTIDO:";
invertir(nro);
cout<<endl<<endl;
return 0;
}
/* Autor: Joel Cesar Fernandez Segura
Fecha: 28/08/2014
Tema: Recursividad
Ejercicio 1: calcula el MCD de dos numeros
IDE: CodeBlocks
Pagina Web: http://codebotic.blogspot.com
*/
#include<iostream>
#include<cstdlib>
#include<string.h>
using namespace std;
int mcd(int a, int b){
if(b==0) return a;
else mcd(b,a%b);
}
int main( void ){
system("color 0a");
int a,b;
cout<<"\n\t\t[ RECURSIVIDAD ]\n";
cout<<"\t\t------------------------\n\n";
cout<<" EJERCICIO 8: Calcula el MCD de dos numeros "<<endl<<endl;
/*Validamos que "a" sea positivo y entero*/
do{
cout<<" INGRESE PRIMER NUMERO: ";
cin>>a;
if(a<0) cout<<"\nINGRESE UN NUMERO ENTERO Y POSITIVO... \n";
}while(a<0);
/*Validamos que "b" sea positivo y entero*/
do{
cout<<" INGRESE SEGUNDO NUMERO: ";
cin>>b;
if(b<0) cout<<"\nINGRESE UN NUMERO ENTERO Y POSITIVO... \n";
}while(b<0);
/*si a>b llama a mcd(a,b)*/
if(a>b)
cout<<"\n MDC( "<<a<<" , "<<b<<" ) = "<<mcd(a,b)<<endl<<endl;
else
if(a<b) /*si b>a llama a mcd(b,a)*/
cout<<"\n MDC( "<<b<<" , "<<a<<" ) = "<<mcd(b,a)<<endl<<endl;
return 0;
}
/* Autor: Joel Cesar Fernandez Segura
Fecha: 28/08/2014
Tema: Recursividad
Ejercicio 3: comprueba si una palabra es palindroma
IDE: CodeBlocks
Pagina Web: http://codebotic.blogspot.com
*/
#include<iostream>
#include<cstdlib>
#include<string.h>
using namespace std;
int palindroma(char palabra[],int ini, int fin){
if(ini >= fin)
return 1;
if(palabra[ini] == palabra[fin])
palindroma(palabra, ini+1, fin-18);
else return 0;
}
int main( void ){
system("color 0a");
char palabra[50];
int tam,pal;
cout<<"\n\t\t[ RECURSIVIDAD ]\n";
cout<<"\t\t------------------------\n\n";
cout<<" EJERCICIO 8: Comprueba si una cadena es palindroma "<<endl<<endl;
cout<<" INGRESE PALABRA: ";
cin.getline(palabra,50);
tam=strlen(palabra);
pal=palindroma(palabra,0,tam-1);
if(pal==1)
cout<<"\n La palabra ES palindroma\n";
else if(pal==0)
cout<<"\n\n la palabra NO es Palindroma: \n";
return 0;
}
/* Autor: Joel Cesar Fernandez Segura
Fecha: 28/08/2014
Tema: Recursividad
Ejercicio 3: Calcular la potencia de n
IDE: CodeBlocks
Pagina Web: http://codebotic.blogspot.com
*/
#include<iostream>
#include<cstdlib>
using namespace std;
long int potencia(int base,int e){
if(e==0) return 1;
if(e==1) return base;
else return base*potencia(base,e-1);
}
int main( void ){
system("color 0a");
int b,e;
cout<<"\n\t\t[ RECURSIVIDAD ]\n";
cout<<"\t\t------------------------\n\n";
cout<<" EJERCICIO 2: Calcular la Potencia de n "<<endl<<endl;
cout<<" INGRESE BASE: ";
cin>>b;
do{
cout<<" INGRESE EXPONENTE: ";
cin>>e;
if(e<0) cout<<"\nINGRESE UN NUMERO ENTERO Y POSITIVO... \n";
}while(e<0);
cout<<endl;
cout<<"\n Base:"<<b;
cout<<"\n Exp:"<<e;
cout<<"\n\n RESULTADO: "<<potencia(b,e)<<endl<<endl;
return 0;
}
/* Autor: Joel Cesar Fernandez Segura
Fecha: 28/08/2014
Tema: Recursividad
Ejercicio 4: Calcular el producto de dos numeros
IDE: CodeBlocks
Pagina Web: http://codebotic.blogspot.com
*/
#include<iostream>
#include<cstdlib>
using namespace std;
int producto(int a,int b){
if(a==0||b==0) return 0;
if(a==1) return b;
if(b==1) return a;
else return a+producto(a,b-1);
}
int main( void ){
system("color 0a");
int a,b;
cout<<"\n\t\t[ RECURSIVIDAD ]\n";
cout<<"\t\t------------------------\n\n";
cout<<" EJERCICIO 3: Calcular el Producto de 2 numeros "<<endl<<endl;
cout<<" INGRESE MULTIPLICANDO: ";
cin>>a;
cout<<" INGRESE MULTIPLICADOR: ";
cin>>b;
cout<<endl;
cout<<"\n MDO:"<<a;
cout<<"\n MDOR:"<<b;
cout<<"\n\n RESULTADO: "<<producto(a,b)<<endl<<endl;
return 0;
}
/* Autor: Joel Cesar Fernandez Segura
Fecha: 28/08/2014
Tema: Recursividad
Ejercicio 2: Suma de los n primeros numeros naturales
IDE: CodeBlocks
Pagina Web: http://codebotic.blogspot.com
*/
#include<iostream>
#include<cstdlib>
using namespace std;
int suma(int nro){
if(nro==0) return 0;
if(nro==1) return 1;
else return nro+suma(nro-1);
}
int main( void ){
system("color 0a");
int nro;
cout<<"\n\t\t[ RECURSIVIDAD ]\n";
cout<<"\t\t------------------------\n\n";
cout<<" EJERCICIO 2: Suma de N primeros Numeros Naturales"<<endl<<endl;
cout<<" INGRESE CANTIDAD DE TERMINOS:";
cin>>nro;
cout<<endl<<endl;
for(int i=1;i<nro+1;i++){ //Mostramos la sucesion de terminos
cout<<i;
if(i<nro) cout<<" + ";
}
cout<<"\n\nLa Suma es: "<<suma(nro)<<endl<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment