Program of tower of Hanoi

Started by Kalyan, Jul 26, 2008, 02:56 PM

Previous topic - Next topic

Kalyan

Program of tower of Hanoi

#include
#include
int main()
{
int n;
char beg='a',aux='b',end='c';
void tower(int,char,char,char);
clrscr();
cout<<"Enter the no. of disks:";
cin>>n;
tower(n,beg,aux,end);
getch();
return(0);
}
void tower(int n,char beg,char aux,char end)
{
if(n==1)
{
cout << beg <<"->" << end<<"\t";
return;
}
tower(n-1,beg,end,aux);
cout << beg<<"->" << end<<"\t";
tower(n-1,aux,beg,end);
}


OUTPUT:

Enter the no. of disks: 3

a->c a->b c->b a->c b->a b->c a->c