#include < stdio.h >
long int n;
void hanoi(char,char,char,int);
void main()
{
char p1='1',p2='2',p3='3',ch;
int nd;
do
{
clrscr();
printf("TOWER OF HANOI\n\n");
printf("Enter number of disks : ");
scanf("%d",&nd);
n=0;
hanoi(p1,p2,p3,nd);
printf("Total number of operation performed to transfer %d disks = %ld\n",nd,n);
printf("\n\n\nDo you want to continue (y/n?) : ");
ch=getch();
}while(ch=='y'||ch=='Y');
}
void hanoi(char p1,char p2,char p3,int nd)
{
if(nd==1)
{
n++;
printf("Move disk = %d from Peg %c to Peg %c\n",nd,p1,p2);
return;
}
hanoi(p1,p3,p2,nd-1);
n++;
printf("Move disk = %d from Peg %c to Peg %c\n",nd,p1,p2);
hanoi(p3,p2,p1,nd-1);
}
long int n;
void hanoi(char,char,char,int);
void main()
{
char p1='1',p2='2',p3='3',ch;
int nd;
do
{
clrscr();
printf("TOWER OF HANOI\n\n");
printf("Enter number of disks : ");
scanf("%d",&nd);
n=0;
hanoi(p1,p2,p3,nd);
printf("Total number of operation performed to transfer %d disks = %ld\n",nd,n);
printf("\n\n\nDo you want to continue (y/n?) : ");
ch=getch();
}while(ch=='y'||ch=='Y');
}
void hanoi(char p1,char p2,char p3,int nd)
{
if(nd==1)
{
n++;
printf("Move disk = %d from Peg %c to Peg %c\n",nd,p1,p2);
return;
}
hanoi(p1,p3,p2,nd-1);
n++;
printf("Move disk = %d from Peg %c to Peg %c\n",nd,p1,p2);
hanoi(p3,p2,p1,nd-1);
}
No comments:
Post a Comment