Tower of Hanoi


Program with recurrsion.. TOWER OF HANOI prob...


#include<stdio.h> 
#include<conio.h>
void towers(int,char,char,char);
int count=0;
main()
{
int n;
clrscr();
printf("\n Enter the no of disks:");
scanf("%d",&n); 
towers(n,'a','c','b');
printf("\nTotal no of moves required is %d",count);
getch();
return 0;
}
void towers(int n,char frompeg,char topeg,char temppeg)
{
if(n==1) 
{
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
count++;
return;
}
towers(n-1,frompeg,temppeg,topeg);
count++;
printf("Move disk 1 from peg %c to peg %c\n",frompeg,topeg);
towers(n-1,temppeg,topeg,frompeg); 
}
Category: ,

2 comments:

DEEPAK said...

superb........thnks yaar for this pgm

Adi said...

Yo welcome.....!