Solved programming problem

Tower of Hanoi in C++.

Source code

#include <iostream>

using namespace std;

void tower(int n,char A,char C,char B)

{

    if(n==1)

    {

        cout<<"Move Ring "<<n<<" :"<<A<<" to "<<C<<endl;

        return ;

    }

    tower(n-1,A,B,C);

    cout<<"Move Ring "<<n<<" :"<<A<<" to "<<C<<endl;

    tower(n-1,B,C,A);

}

int main()

{

   int n;

   cout<<"Please Enter Total disks:";

   cin>>n;

   tower(n,'A','C','B'); ///send the disks number and three sample tower

   return 0;

}

Output

Please Enter Total disks:3

Move Ring 1 :A to C

Move Ring 2 :A to B

Move Ring 1 :C to B

Move Ring 3 :A to C

Move Ring 1 :B to A

Move Ring 2 :B to C

Move Ring 1 :A to C

Join the discussion

Add your comment