RECURSIVIDAD -lógica de programación- | Page 6

Programación en C - Funciones - Función recursiva

#include <iostream>

#include <cstdlib>

using namespace std;

int hanoi(int n, char origen, char destino, char auxiliar);

int main(){

int valor;

system("clear");

cout << "Introduzca numero a calcular: ";

cin >> valor;

hanoi(valor,'A','B','C');

return 0;

}

int hanoi(int n, char origen, char destino, char auxiliar){

if (n <= 1){

cout << "\nmover disco 1 del poste "<< origen << " al poste " << destino << endl;

}else {

hanoi(n-1, origen, auxiliar, destino);

cout << "\nmover disco " << n << " del poste " << origen << " al poste "<< destino;

hanoi (n-1, auxiliar, destino, origen);

}

}

PRO

EKEMPLO: PROGRAMA CON FUNCION RECURSIVA EN C++