Industrial Training




Program of towers of hanoi



/* Program of towers of hanoi. */

Tower Of Hanoi

Tower of hanoi is a historical problem, which can be easily expressed using recursion. There are N disks of decreasing size stacked on one needle, and two other empty needles. It is required to stack all the disks onto a second needle in the decreasing order of size. The third needle can be used as a temporary storage. The movement of the disks must confirm to the following rules,

1. Only one disk may be moved at a time
2. A disk can be moved from any needle to any other.
3. The larger disk should not rest upon a smaller one.

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

void move ( int, char, char, char ) ;

void main( )
{
int n = 3 ;

clrscr( ) ;

move ( n, 'A', 'B', 'C' ) ;

getch( ) ;
}

void move ( int n, char sp, char ap, char ep )
{
if ( n == 1 )
printf ("\nMove from %c to %c ", sp, ep ) ;
else
{
move ( n - 1, sp, ep, ap ) ;
move ( 1, sp, ' ', ep ) ;
move ( n - 1, ap, sp, ep ) ;
}
}



Hi I am Pluto.