Created
May 8, 2014 06:05
-
-
Save sarathsp06/f2f51963c2212dcc7acb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tower of hanoi , a classical example on how uselfull is recursion to simplify the coding