Sunday 7 April 2013

TOWERS OF HANOI


#include<stdio.h>
#include<conio.h>

void hanoi(int, char, char, char);
void main()
{
    int disks;
    char source, destination, aux;
    clrscr();
    printf("\nEnter No. of Disks:");
    scanf("%d",&disks);
    printf("\nEnter Source, Destination and Auxillary Disk Names:");
    scanf("%s%s%s",source, destination, aux);
    hanoi(disks,source,destination,aux);
    getch();
}
 void hanoi(int n, char s, char d, char a)
{
    if(n>0)
    {
        hanoi(n-1,s,a,d);
        printf("\n%d is moved from %s to %s",n,s,d);
        hanoi(n-1,a,d,s);
    }

}
Output


Enter No. of Disks:3

Enter Source, Destination and Auxillary Disk Names:a
b
c

1 is moved from a to b 
2 is moved from a to c
1 is moved from b to c
3 is moved from a to b 
1 is moved from c to a
2 is moved from c to b 
1 is moved from a to b 

No comments:

Post a Comment