/* A 'C++' PROGRAM TO SOLVE THE TOWERS OF HANOI PROBLEM USING RECURSIVE FUNCTIONS
*/
#include<iostream.h>
#include<conio.h>
void hanoi(int n,char s,char t,char d)
{
if(n!=0)
{
hanoi(n-1,s,d,t);
cout<<"\nMOVE DISK "<<n<<" FROM "<<s<<" TO "<<d;
hanoi(n-1,t,s,d);
}
}
void main()
{
int n;
char s='A',t='B',d='C';
clrscr();
cout<<"Enter how many disks :";
cin>>n;
cout<<"\t\t\tTOWERS OF HANOI"<<"\n\t\t\t---------------";
hanoi(n,s,t,d);
getch();
}
/* OUTPUT
------
Enter how many disks :3
TOWERS OF HANOI
---------------
MOVE DISK 1 FROM A TO C
MOVE DISK 2 FROM A TO B
MOVE DISK 1 FROM C TO B
MOVE DISK 3 FROM A TO C
MOVE DISK 1 FROM B TO A
MOVE DISK 2 FROM B TO C
MOVE DISK 1 FROM A TO C
*/
Thanks....your blog was helpful
ReplyDelete