- Related Questions & Answers
- C Program for Tower of Hanoi
- C++ Program for Dijkstra’s shortest path algorithm?
- C/C++ Program for nth Catalan Number?
- C Program to Multiply two Floating Point Numbers?
- C program to find the length of a string?
- C Program to Check if all digits of a number divide it
- C Program to Check if a Given String is a Palindrome?
- Write a program to Delete a Tree in C programming
- Write a C program to print “ Tutorials Point ” without using a semicolon
- Sum of the nodes of a Singly Linked List in C Program
- Sum of first n natural numbers in C Program
- C Program for simple interest?
- Area of a n-sided regular polygon with given Radius in C Program?
- Area of a leaf inside a square in C Program?
- C Program for Area of a square inscribed in a circle which is inscribed in a hexagon?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this article, we will learn about the solution to the problem statement given below.

**Problem statement **− We are given n disks and a series of rods, we need to transfer all the disks to the final rod under the given constraints−

We can move only one disk at a time.

Only the uppermost disk from the rod can be moved.

Any bigger disk cannot be placed on the smaller disk

Now let’s observe the solution in the implementation below −

# tower of hanoi def TowerOfHanoi(n , from_rod, to_rod, aux_rod): if n == 1: print ("Move disk 1 from rod",from_rod,"to rod",to_rod) return TowerOfHanoi(n-1, from_rod, aux_rod, to_rod) print ("Move disk",n,"from rod",from_rod,"to rod",to_rod) TowerOfHanoi(n-1, aux_rod, to_rod, from_rod) # main n = 3 TowerOfHanoi(n, 'A', 'C', 'B') # A, B, C are the rod print ("Sorted array is:") for i in range(n): print (arr[i],end=" ")

Move disk 1 from rod A to rod C Move disk 2 from rod A to rod B Move disk 1 from rod C to rod B Move disk 3 from rod A to rod C Move disk 1 from rod B to rod A Move disk 2 from rod B to rod C Move disk 1 from rod A to rod C

All the variables are declared in the local scope and their references are seen in the figure above.

In this article, we have learned about how we can make a Python Program for Tower of Hanoi

Advertisements