Recursion : Tower of Hanoi in C++

Tower of Hanoi by recursion

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout<<"Enter the number of disks inTOH:";
    cin>>n;
    move(n,'A','C','B');
    system("pause");
    return 0;
}
void move(int n,char x,char z,char y)
{
    if(n==1) cout<<"Move disk 1 from "<<x<<" to "<<z<<endl;
    else
    {
        move(n-1,x,y,z);
        cout<<"Move disk "<<n<<" from "<<x<<" to"<<z<<endl;
        move(n-1,y,z,x);
    }
}
   


No comments :

Post a Comment

Your Comment and Question will help to make this blog better...