Skip to content

Instantly share code, notes, and snippets.

@sarathsp06
Created May 8, 2014 06:05
Show Gist options
  • Save sarathsp06/f2f51963c2212dcc7acb to your computer and use it in GitHub Desktop.
Save sarathsp06/f2f51963c2212dcc7acb to your computer and use it in GitHub Desktop.
#include<iostream>
using namespace std;
void move(int n,char* src,char* dest,char* buffer)
{
if(n==2){
cout<<"\n1,\t"<<src<<" -->"<<buffer;
cout<<"\n2,\t"<<src<<" -->"<<dest;
cout<<"\n1,\t"<<buffer<<" -->"<<dest;
}
else{
move(n-1,src,buffer,dest);
cout<<"\n"<<n<<",\t"<<src<<" -->"<<dest;
move(n-1,buffer,dest,src);
}
}
int main(){
int n;
cout<<"Enter the number of disks";
cin>>n;
move(n,"Source","Destination","Buffer");
cout<<"\nDone\n"<<endl;
}
@sarathsp06
Copy link
Author

Tower of hanoi , a classical example on how uselfull is recursion to simplify the coding

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment