Monday, May 4, 2009

PROGRAM TO IMPLEMENT TOWERS OF HANOY

#include
#include
void towers(int n,char source,char dest,char auxilary);
void main()
{
int n;
char s='a',d='c',a='b';
clrscr();
printf("\nEnter the value of n\n");
scanf("%d",&n);
towers(n,s,d,a);
getch();
}
void towers(int n,char source,char dest,char auxilary)
{
static int step=0;
printf("Towers(n=%d,source=%c,dest=%c,aux=%c)\n",n,source,dest,auxilary);
if(n==1)
printf("\t\t\t\t\tstep %3d:Move from %c to %c\n",++step,source,dest);
else
{
towers(n-1,source,auxilary,dest);
printf("\t\t\t\t\tstep %3d:Move from %c to %c\n",++step,source,dest);
towers(n-1,auxilary,dest,source);
}
}

No comments:

Post a Comment