Posts

Showing posts from March, 2011

Tower of Hanoi using Recursive Function

Try it Out #include<iostream> using namespace std; int Hanoi(int nob, char source, char target, char help) { if(nob == 0) return 1; Hanoi(nob - 1, source, help, target); // help and target are swapped cout<<"Moving "<<nob<<" : "<<source; cout<<" --> "<<target<<endl; Hanoi(nob - 1, help, target, source); // help and source are swapped } int main() { Hanoi( 3, 'A', 'C', 'B'); return 0; } Output Moving 1 : A --> C Moving 2 : A --> B Moving 1 : C --> B Moving 3 : A --> C Moving 1 : B --> A Moving 2 : B --> C Moving 1 : A --> C