} C++ Program for Tower Of Hanoi #include Moving the disks on top of already moved first disk ("Moving disk "+n+"From rod:"+from+"To Rod"+to) Public static void hanoi(int n,char from,char mid,char to)
JAVA Program for Tower Of Hanoi public class hanoi Let us now look at a recursive implementation of the same. We are thus moving n-1 disks on to the second tower, the last disk to the third tower and n-1 disks onto the first disk thus completing the shift. We would need 7 steps to shift all of them to the third ring. (3 moves)Īssumption: The disks are initially sorted We move the top(small) disk onto the next tower after which we move the second disk to the third tower and then eventually shift the first disk as well on to the third tower. Let us look at how this problem can be handled when we have two disks