Tower of Hanoi :

<br />#include<iostream>
#include<conio.h>
using namespace std;
void tower(int n,char beg,char aux, char end)
{
if(n==1)
{
cout<<"\nMove disk "<<n<<" from "<<beg<<" to "<<end;
}
else if(n<=0)
{
cout<<"\nInvalid input";
}
else
{
tower(n-1,beg,end,aux);
cout<<"\nMove disk "<<n<<" from "<<beg<<" to "<<end;
tower(n-1,aux,beg,end);
}
}
main()
{
int n;
cout<<"Enter elements:";
cin>>n;
tower(n,'A','B','C');
getch();
}

Output:-
Tower

Leave a comment