C Interviewquestions
C Interviewquestions
2. First move the n-1 rings from first to a second pole using the third.
3. Then move the remaining big ring from first to the third.
4. Now move all the n-1 rings from the second pole to the target third pole on top of the nth (which is big).
5. For moving the n-1 rings from one pole to another pole, use the remaining pole as a buffer using recursion.
C++ program to to solve the Towers of Hanoi game
http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 1/4
9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks
#include <iostream>
using namespace std;
#define MAX 4
int fromPoleMain[MAX] = {4,3,2,1};
int tmpPoleMain[MAX];
int toPoleMain[MAX];
return;
}
for(j = 0; j<MAX;j++)
{
if(toPole[j] == 0)
break;
}
toPole[j] = fromPole[i-1];
printPole(fromPole);
cout << ": posi " << i << " to --> ";
printPole(toPole);
cout << ": at posi " << j ;
cout << " moving " << toPole[j] <<endl;
fromPole[i-1] = 0;
int main()
{
printPole(fromPoleMain); printPole(toPoleMain); printPole(tmpPoleMain); cout << ": " << endl;
TowersOfHanoi(MAX,fromPoleMain,toPoleMain,tmpPoleMain);
cout << "Finally target POLE: ";
for(int i = 0; i<MAX;i++)
{
cout << toPoleMain[i] << " ";
}
cout << endl;
return 0;
}
Output:-
http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 2/4
9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks
Related Posts:
Linked list nth last element (http://www.sourcetricks.com/2012/07/linked-list-nth-last-element.html)
Write a program to find in a linked list nth last element The approach:- Maintain 2 pointers to head of the list. Move 1st pointer n - 1 elements. Now move… Read More
(http://www.sourcetricks.com/2012/07/linked-list-nth-last-element.html)
2 comments :
Reply
red hat linux training in chennai (http://sysacad.in/portfolio-view/linux-training-in-chennai/) | rhce courses in chennai (http://sysacad.in/portfolio-view/linux-training-in-
chennai/) | red hat training in chennai (http://sysacad.in/portfolio-view/linux-training-in-chennai/) |red hat courses in chennai (http://sysacad.in/portfolio-view/vmware-
training-in-chennai/)
Reply
http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 3/4
9/30/2017 Towers of Hanoi ~ Programming Tutorials by SourceTricks
(https://www.blogger.com/comment-iframe.g?blogID=7748177500667831327&postID=861168250104027697&blogspotRpcToken=3526398)
Tutorial Pages
Android (http://www.sourcetricks.com/p/android.html)
Tag Cloud
http://www.sourcetricks.com/2014/01/towers-of-hanoi.html#.Wc-o-miCzIU 4/4