/**************************************************************************

-> This C++ program is to solve the towers of hanoi problem.

-> Implemented using recursion.

-> Works in Microsoft VC++ 6.0 , windows xp.

-> Header files used 1)iostream.h

***************************************************************************/

#include<iostream.h>

void move(int n,char *s,char *i,char *d)

// s stands for source tower

// d stands for destination tower

// i stands for intermediate tower

{

if(n>0)

{

move(n-1,s,d,i);

// move n-1 disks from source to intermediate tower

cout<<“disk “<<n<<” is moved from “<<s<<” to “<<d<<endl;

// move the disk from to source to destination

move(n-1,i,s,d);

// move n-1 disks from intermediate to destination

}

}

void main()

{

cout<<“\n**********************************************************\n”;

cout<<“This C++ program is to solve the towers of hanoi problem”;

cout<<“\n**********************************************************\n”;

cout<<“Enter the no. of disks “;

int n;

cin>>n;

move(n,”source tower”,”intermediate tower”,”destination tower”);

}

great prog. dude ..thanks … can u post the explanation also ..

i m new to recursion tech.

c using linked list in towers of honai

Goodness,

thank God you posted such program… you know i do really need it on our major subject, it is just difficult for me to understand…but thanks anyway for the post…it helped me so much…

thanx alot 4r ur this post……….really helpfull to many like me who r new to recursion……..

can u explain me dis……..??

Recursive solution

A key to solving this puzzle is to recognize that it can be solved by breaking the problem down into a collection of smaller problems and further breaking those problems down into even smaller problems until a solution is reached. The following procedure demonstrates this approach.

label the pegs A, B, C—these labels may move at different steps

let n be the total number of discs

number the discs from 1 (smallest, topmost) to n (largest, bottommost)

To move n discs from peg A to peg C:

move n−1 discs from A to B. This leaves disc n alone on peg A

move disc n from A to C

move n−1 discs from B to C so they sit on disc n

The above is a recursive algorithm: to carry out steps 1 and 3, apply the same algorithm again for n−1. The entire procedure is a finite number of steps, since at some point the algorithm will be required for n = 1. This step, moving a single disc from peg A to peg B, is trivial. This approach can be given a rigorous mathematical formalism with the theory of dynamic programming,and is often used as an example of recursion when teaching programming.

Thanks alot mate… I spent almost 2 hours on this problem but couldn’t really come to the solution…. thanks 2 u for this little explanation…. I got it in 2 mins, watever thanx alot……….

i need solution for “TOWER OF HANOI” in data structure & algorithm…for my assignment

hey i just downt find a quelltext

thank you so much for making this program..it really help me in my assingment..thank God..i cant find it anwhere,only here..thank you!!!

fuck you bitch im glen john from phillipines

fuck you bitch im glen john from phillipines is this tower of nuynoy??

youre the bitch!

no your the bitch.. i bet you dont even get what recursion is.. this post is a big help for us who want to learn the basics of complex and efficient programming.. shame on you mindless person.. im from the Philippines to and im ungrateful to have a fellow Filipino citizen like you snooping around with that attitude..

Thanks for this program. Please guide me for my future problem

thank you

tremands idea to rectfiy our problems

could sumone tell me de solution for tower of hanoi without use of recursion….using iteration??plsssssssss

thanks a lot sir..!! i relly needed this program

i need tower of hanoi solution in c++ with graphics ………..

plz help me out

have u a graphical representation of tower of hanoi in ds?

plz reply.

it is important for me.

i want these in 2 days.

oh… it really helped me a lot… i have xam 2mrw n i had no idea of it… n its ma major course too…

thanks for only uuuuuuuuuu.

realy help with me , in this…………

thanks for your program!!!!!!

include

void move(int n,char *s,char *i,char *d)

// s stands for source tower

// d stands for destination tower

// i stands for intermediate tower

{

if(n>0)

{

move(n-1,s,d,i);

// move n-1 disks from source to intermediate tower

cout<<”disk “<<n<<” is moved from “<<s<<” to “<<d<<endl;

// move the disk from to source to destination

move(n-1,i,s,d);

// move n-1 disks from intermediate to destination

}

}

void main()

{

cout<<”\n**********************************************************\n”;

cout<<”This C++ program is to solve the towers of hanoi problem”;

cout<<”\n**********************************************************\n”;

cout<>n;

move(n,”source tower”,”intermediate tower”,”destination tower”);

}

Am giving a projest title An algorithm of cyclic tower of Hanoi.

Please how do I go about it?

wat r the values of s,i,d???

‘Towers of Hanoi’ Puzzle is explained in great detail with both the

– Recursive & Non-Recursive solution and

– Time & Space complexities

at the below link

http://www.rawkam.com/?p=917

can u explain why have you used pointers while taking tower…… I mean what is the purpose for using that…. Reply soon….

thank u so muchhhhhhh………………………….

Neenu from Kannur(pappinisseri)

thank you so much dudeeeeeeeeeee

Neenu from Kannur(pappinisseri)

Thank you so much babaaaaaaaaa

Neenu from Kannur(pappinisseri)

And also i want algorithm of TOWER OF HANOI

SO plsss how do i go about it???????

NEENU FROM KANNUR(PAPPINISSERI)

1.Algorithm TowersOfHanoi(n,x,y,z)

2.//Move the top n diska from tower x to y using tower z

3.{

4. if(n>=1)

5. {

6. TowersOfHanoi(n-1,x,z,y);

7. write(“Move top disk from tower”,x,”to tower”,y);

8. TowersOfHanoi(n-1,z,y,x);

9. }

10.}

you are also provide explanation

10Q U very much!!

this is good

ye sub faltu hai koi mat padna

Made code work cross-platform (debugged):

/* Tower of Hanoi */

#include

using namespace std;

void move (int ringNum, char *first, char *second, char *third)

{

if (ringNum > 0)

{

move(ringNum-1, first, third, second); // move ringNum-1 disks from 1st tower to 2nd tower

cout << "Disk " << ringNum << " is moved from " << first << " tower to " << third << " tower." << endl;

// move the disk from to 1st input (*first) to 3rd input (*third)

move(ringNum-1, second, first, third); // move ringNum-1 disks from 2nd tower to 3rd tower

}

return;

}

`int main()`

{

int ringNum = 0;

cout <> ringNum;

move (ringNum, "1st", "2nd", "3rd");

return 0;

}

LOL, the site messed up my code worse than the original…let’s see if this is any better…

One last try:

which data structures used in Tower of Hanoi

how to calculate time compleixity in TOH…

can i implement using gready method in tower of hanoi prog….

if you please write the same program in ‘c’ language using the ‘c’ syntax;

i m commanded by my teacher to show the visualize of tower of henoi using data structure(“c+”)..plz tell me how can i do that? and which will be the code for that of using GUI?

Dude! Look at may 7th 2011 its in c++. You must be extremely new to programming and this is already in recursion. If you want it in a more basic data structure it would be even simpler then this. So if your posting this kind of work for a basic data structure your instructor will know the difference. This is recursion which is n -1 which uses a tree. and the variable ringNum which is the max amount of rings and the bottom and 1 which is the least is at your top.

great code…simple and logical

#include

#include

main()

{

int n;

void tower(char,char,char,int);

printf(“Enter the number of disk”);

scanf(“%d”,&n);

tower(‘i’,’j’,’k’,n);

getch();

}

void tower(char a,char b,char c,int n)

{

if(n==1)

printf(“\nMove disk from %c to %c “,a,c);

else

{

tower(a,c,b,n-1);

tower(a,b,c,1);

tower(b,a,c,n-1);

}

}

i want algorithm for this

plz i want in simplest logical way

very good program. Logically sound. Thanx bro

it isa good pgm.it helps me very much. please give me the algorithm for tower of hanoi.

I want explanation of this program

Have a look here for explanation

http://clanguagestuff.blogspot.in/2013/09/tower-of-hanoi.html