DEV Community

Swapnil Gupta
Swapnil Gupta

Posted on

Tower of Hanoi: Recursion Problem

problem: three poles having disk in it , A-B-C, you have to move all disk from A to C using B as helper.

  • arranged in manner that larger at bottom and smaller at top, so smaller should not be lower to larger one.
  • print all steps to move all the disk Image description

we can also write :
move n-1 discs from A to B using C
move a disc from A to C
move n-1 Discs from B to C using A

void TowerOfHanoi(int n, Int A, int B, Int C  ){
   TowerOfHanoi(n-1, A, C, B);
printf("Moving disc from %d to %d", A, C);

Enter fullscreen mode Exit fullscreen mode

for (n-1) or 1 disk it is easy to move,

Image description

solution Reference: Tower of Hanoi Problem - Made Easy

Top comments (0)