This is a famous game for programmer .
Object of the game is to move all the disks over to tower C (final destination let say D ) from tower A (source tower let say S ) with the help of tower B (temporary Tower let say T) .
But with a simple C program ,we can solve it.
This is also an perfect example of recursion
OUTPUT:
Object of the game is to move all the disks over to tower C (final destination let say D ) from tower A (source tower let say S ) with the help of tower B (temporary Tower let say T) .
Condition : But here is the twist,you cannot place a larger disk onto a smaller disk at any time during your moves.
Related Post : Test
your Programming Skill : P-1
This is also an perfect example of recursion
C program:
- /* Program for solution of Tower of Hanoi*/
- #include<stdio.h>
- #include<conio.h>
- void toh( int ndisk, char source, char temp, char dest )
- {
- if ( ndisk > 0 )
- {
- toh ( ndisk-1, source, dest, temp );
//first move n-1 disks from source to temp - printf ( "Move Disk %d %c-->%c\n", ndisk, source,dest );
- toh( ndisk-1, temp, source, dest );
//move remaining one disk to destination - }
- }/*End of toh()*/
- int main()
- {
- char source= 'S',temp= 'T', dest= 'D';
- int ndisk;
- printf("Enter the number of disks : "); //no. of disks
- scanf ( "%d", &ndisk );
- printf ("Sequence is :\n");
- toh( ndisk, source, temp, dest ); //passing values to each argument
- getch();
- }
Related Post : Php
crash course Get POST & REQUEST
thank you.................
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT