Header Ads Widget

Tower of hanoi by python

 tower of hanoi :-

Rule:-

                         The problem is to move all the disk from source to destination by the help of Via . A few rules to be followed for Tower of Hanoi are −
  • 1) Only one disk can be moved among the towers at any given time.
  • 2) Only the "top" disk can be removed.
  • 3) No large disk can sit over a small disk.

Code:-


#function for tower of hanoi
def TOH(n,a,b,c):
if n==1:
print(a,"to",c)
else:
TOH(n-1,a,c,b)
TOH(1,a,b,c)
TOH(n-1,b,a,c)
n=int(input("Enter number of disk\n"));
TOH(n,"A","B","C")


Output:-


Enter number of disk
3
A to C
A to B
C to B
A to C
B to A
B to C
A to C



Post a Comment

3 Comments