Tower Of Hanoi

August 13, 2010

Cool C Programs

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. Tower of hanoi 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.

/* Program of towers of hanoi. */

#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 ) ;
}
}

Subscribe

Subscribe to our e-mail newsletter to receive updates.

One Response to “Tower Of Hanoi”

  1. hamed Says:

    i want merge sort c++ and function

Leave a Reply