Tower of Hanoi

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

void TOH(int n,char a,char b,char c)
{
    if(n>=1)
    {
        TOH(n-1,a,c,b);
        printf("Move top disc from tower %c to top of tower %c\n",a,b);
        TOH(n-1,c,b,a);
    }
}

void main()
{
    int n;
    clrscr();
    printf("Enter number of rings ");
    scanf("%d",&n);
    TOH(n,'x','y','z');
    getch();
}

/*Output
Enter number of rings 3
Move top disc from tower x to top of tower y
Move top disc from tower x to top of tower z
Move top disc from tower y to top of tower z
Move top disc from tower x to top of tower y
Move top disc from tower z to top of tower x
Move top disc from tower z to top of tower y
Move top disc from tower x to top of tower y
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s