Branch: : Aeronautical Engineering
TOWERS OF HANOI
1.Suppose three pegs,labeled as A,B,C.
2.On peg a there are placed n disks with decreasing size.(means top disk is small and bottom disk is larger)
OBJECTIVE:
We have to move the disks from peg A to C using B as auxilary peg.
following that rules:
1.Only one disk can be moved at a time (means only topmost disk from any peg can be moved)
2.At no time a larger disk can be placed on a smaller disk.
Algorithm for towers of hanoi:
Tower(N,A,B,C) (where n is the number of disks.)
the procedure gives a recursive solution for the problem of tower of hanoi.
1.if N=1,then:
a)Write:A->C
b)return.
2.(Move N-1 disks from A->B)
CAll Tower(N-1,A,C,B)
3.Write A->B
4.(Move N-1 disks from B->C)
Call Tower(N-1,B,A,C)
5.Return.
PROGRAME
- #include <stdio.h>
- void towers(int, char, char, char);
- int main()
- {
- int num;
- printf("Enter the number of disks : ");
- scanf("%d", &num);
- printf("The sequence of moves involved in the Tower of Hanoi are :");
- towers(num, 'A', 'C', 'B');
- return 0;
- }
- void towers(int num, char frompeg, char topeg, char auxpeg)
- {
- if (num == 1)
- {
- printf("Move disk 1 from peg %c to peg %c", frompeg, topeg);
- return;
- }
- towers(num - 1, frompeg, auxpeg,topeg);
- towers(num - 1, auxpeg, topeg, frompeg);
- }
Similar Threads:
Last edited by ajaytopgun; 3rd April 2014 at 11:38 AM.
Branch: : Aeronautical Engineering
so what u want u ask?????be specific......
Branch: : Aeronautical Engineering
thanks for sharing this algorithm..
Gender: : Male
Branch: : Computer Science Engineering
City : Chandigarh*
What you want to ask in tower of hanoi problem? Be specific.