To solve the towers of Hanoi problem by using
the recursive function
#include<conio.h>
void Hanoirecursion(int num,char ndl1,char ndl2,char ndl3)
{
if(num==1)
{
printf("Move top disk from needle %c to
needle %c",ndl1,ndl2);
return;
}
Hanoirecursion(num-1,ndl1,ndl3,ndl2);
printf("Move top dis from needle %c to
needlle %c",ndl1,ndl2);
Hanoirecursion(num-1,ndl3,ndl2,ndl1);
}
void main()
{
int no;
clrscr();
printf("Enter the no. of disk to be
transferred:");
scanf("%d",&no);
if(no<1)
printf("\n There's nothing to
move");
else
printf("\n recursive");
Hanoirecursion(no,'A','B','C');
getch();
}
No comments:
Post a Comment