TOWERS OF HANOI


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

  -> 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”);
}

52 thoughts on “TOWERS OF HANOI”

  1. 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…

  2. thanx alot 4r ur this post……….really helpfull to many like me who r new to recursion……..
    can u explain me dis……..??

    1. 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.

      1. 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……….

    1. 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..

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

  4. 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”);
    }

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

    1. 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.}

  6. 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;
    }

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

      #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;
      }

  7. One last try:

    /* Tower of Hanoi */
    
    #include <iostream>
    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 << "Enter the # of disks: ";
        cin >> ringNum;
        move (ringNum, "1st", "2nd", "3rd");
        return 0;
        }
    
  8. 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….

  9. 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?

  10. 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.

  11. #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);
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s