Data Structure • Chapter 30 • Learning Notebook
Tower of Hanoi Using Recursion: Algorithm, C Program & Examples
One puzzle, three rods and one powerful idea. Understand the rules, recursive logic, every move for three disks, call stack, C program, complexity and university exam answers in simple English.
Many students can remember the definition of recursion but find Tower of Hanoi difficult when the teacher asks them to write all moves in the correct order. The puzzle looks simple because there are only three rods and a few disks. The tricky part is that we cannot move any disk whenever we want.
In this chapter, we will first understand the puzzle without programming. Then we will solve it using two and three disks, build the recursive algorithm, write a complete C program and analyse its time and space complexity. The aim is to understand every step rather than memorise the code.
Quick Answer
Tower of Hanoi is a mathematical puzzle solved using recursion. We move all disks from a source rod to a target rod, using one helper rod, without placing a larger disk on a smaller disk. The minimum number of moves for n disks is 2n − 1.
What Is Tower of Hanoi in Data Structure?

Tower of Hanoi is a puzzle that normally has three rods and several circular disks of different sizes. All the disks begin on one rod. The largest disk is at the bottom and the smallest disk is at the top. The task is to move the complete tower to another rod without breaking the rules.
It is not a separate data structure such as an array or stack. It is a classic recursive algorithm problem taught in Data Structure because it demonstrates base cases, smaller subproblems, function calls and exponential time complexity.
Source, Helper and Destination Rod
We name the rods A, B and C. In our examples, A is the source, B is the helper and C is the destination. During recursion their roles change for smaller subtasks. The letters themselves do not matter; the roles do.
How Tower of Hanoi Works with 1 and 2 Disks
One Disk: Only One Move
With one disk, there is no smaller disk to move out of the way. Shift disk 1 directly from A to C. That is the base case for the one-disk explanation and needs only one move.
Two Disks: Three Moves
-
Move disk 1 from A to B.
Keep the smaller disk on the helper rod.
-
Move disk 2 from A to C.
The larger disk is now free to move.
-
Move disk 1 from B to C.
Place it on top of disk 2. The tower is complete.
So two disks need 3 moves. Notice the pattern: move the smaller part away, move the biggest disk and finally put the smaller part on top again.
Tower of Hanoi with 3 Disks: Complete 7-Move Dry Run
Now suppose there are three disks. Disk 1 is the smallest, disk 2 is medium and disk 3 is the largest. We want to move all three from rod A to rod C. We must first clear disk 3, move it to C and then move the two smaller disks onto it.
Follow Every Disk Movement
Move 0 of 7Start: all three disks are on rod A. Read the move table below, or use the controls when available.
The controls work where the Blogger theme permits post JavaScript. The full move table below also works without JavaScript.
All 7 Moves for Three Disks
| Move | Disk | From | To | Why? |
|---|---|---|---|---|
| 1 | 1 (small) | A | C | Start clearing the two smaller disks |
| 2 | 2 (medium) | A | B | Move disk 2 away from the largest |
| 3 | 1 (small) | C | B | Place disk 1 on disk 2 |
| 4 | 3 (large) | A | C | Move the largest disk to the target |
| 5 | 1 (small) | B | A | Free disk 2 on the helper rod |
| 6 | 2 (medium) | B | C | Place disk 2 on disk 3 |
| 7 | 1 (small) | A | C | Complete the tower on rod C |
Final result: Rod A is empty, rod B is empty and rod C contains disks 3, 2 and 1 from bottom to top. Seven moves are required, and that is the minimum possible for three disks.
Why Does Tower of Hanoi Use Recursion?
Recursion means solving a big problem by solving smaller versions of the same problem. Tower of Hanoi follows exactly this idea. To move n disks from source A to target C, we must first move the top n−1 disks out of the way.
-
Move the top n−1 disks from A to B.
Use C as the temporary helper for this smaller problem.
-
Move disk n from A to C.
The biggest disk can now move directly to its destination.
-
Move the n−1 disks from B to C.
Use A as the temporary helper. The tower is complete.
Steps 1 and 3 are smaller Tower of Hanoi problems. We call the same function again with n−1. When there are no disks left, the function stops. This is why a short recursive function can describe a puzzle with thousands of moves.
HANOI(n, source, helper, target):
if n == 0:
return
HANOI(n-1, source, target, helper)
move disk n from source to target
HANOI(n-1, helper, source, target)
The order of the rods in each recursive call is very important. Changing source, helper and target incorrectly produces the wrong sequence of moves.
If the basic idea of a function calling itself is still confusing, revise Recursion in Data Structure: Types, Working and Examples before tracing the Hanoi calls.
Understanding the Base Case
In our program,
if (n == 0) return;
means no disks remain to move.
We could also stop at n = 1 and
print that one move directly.
Both approaches are correct when their
recursive logic matches the chosen
base case.
Tower of Hanoi Using Recursion in C — Complete Program
Below is a complete C program. It accepts a small positive number of disks, prints each legal move and displays the minimum number of moves. We limit the input to 10 so the output stays readable for beginners.
#include <stdio.h>
void hanoi(int n, char source,
char helper, char target) {
if (n == 0) {
return;
}
hanoi(n - 1, source, target, helper);
printf("Move disk %d: %c -> %c\n",
n, source, target);
hanoi(n - 1, helper, source, target);
}
int main(void) {
int n;
printf("Enter number of disks (1 to 10): ");
if (scanf("%d", &n) != 1 ||
n < 1 || n > 10) {
puts("Please enter a number from 1 to 10.");
return 1;
}
printf("Minimum moves: %u\n",
(1U << n) - 1U);
hanoi(n, 'A', 'B', 'C');
return 0;
}
Note:
The expression (1U << n) - 1U
calculates 2n − 1 for our
restricted input range. It should not be
copied unchanged for arbitrarily large n.
Expected Output for n = 3
Enter number of disks (1 to 10): 3
Minimum moves: 7
Move disk 1: A -> C
Move disk 2: A -> B
Move disk 1: C -> B
Move disk 3: A -> C
Move disk 1: B -> A
Move disk 2: B -> C
Move disk 1: A -> C
Line-by-Line Working in Simple Words
-
hanoi(n, source, helper, target)receives the number of disks and three rod labels. -
if (n == 0)is the stopping condition. - The first recursive call shifts the top n−1 disks to the helper rod.
-
printfdisplays the movement of the largest remaining disk. - The second recursive call shifts the smaller disks to the destination rod.
For a beginner explanation of algorithm steps and pseudocode, see Algorithm in Data Structure: Characteristics, Steps and Examples.
Recursion Tree and Call Stack in Tower of Hanoi
The function does not print every move immediately. It first goes deeper to move the smaller disks. For three disks, the outer function makes two calls for two disks, and each of those calls makes two calls for one disk.
HANOI(3, A, B, C)
|
|-- HANOI(2, A, C, B)
| |-- HANOI(1, A, B, C)
| |-- Move disk 2: A -> B
| `-- HANOI(1, C, A, B)
|
|-- Move disk 3: A -> C
|
`-- HANOI(2, B, A, C)
|-- HANOI(1, B, C, A)
|-- Move disk 2: B -> C
`-- HANOI(1, A, B, C)
Each call for one disk also reaches the zero-disk base case before and after its own move. The simplified tree omits those base-case calls to keep the diagram easy to read.
Why Is the Auxiliary Space O(n)?
The call stack stores active recursive calls. At one moment, the calls may go from n disks to n−1, n−2 and so on, down to zero.
The maximum number of active frames grows in proportion to n, so the auxiliary stack space is O(n). We do not store all 2n moves in memory; this program prints each move when it happens.
Minimum Moves, Formula and Time Complexity
To move n disks, we move n−1 disks twice and move the largest disk once. Let T(n) be the minimum number of moves.
With T(0) = 0, the solution is T(n) = 2n − 1.
How Is the Formula Derived?
T(n) = 2T(n−1) + 1. Expand once:
T(n) = 2[2T(n−2) + 1] + 1
T(n) = 4T(n−2) + 3
Continue expanding until T(0) = 0. The result becomes 2n − 1.
| Disks (n) | Calculation | Minimum Moves |
|---|---|---|
| 1 | 2¹ − 1 | 1 |
| 2 | 2² − 1 | 3 |
| 3 | 2³ − 1 | 7 |
| 4 | 2⁴ − 1 | 15 |
| 5 | 2⁵ − 1 | 31 |
| 6 | 2⁶ − 1 | 63 |
| 10 | 2¹⁰ − 1 | 1023 |
Time: O(2n)
The number of moves grows exponentially. This recursive solution performs a constant amount of work for each printed move.
Extra Space: O(n)
The maximum recursive call depth grows linearly with the number of disks.
2^n − 1 does not require
printing those moves.
Do not confuse the complexity of
generating moves with the complexity
of finding the count.
Why Can the Puzzle Not Be Solved in Fewer Moves?
Before moving the largest disk, all n−1 smaller disks must be on the helper rod. After moving the largest disk, those n−1 disks must be moved onto it.
So any valid solution needs at least T(n−1) + 1 + T(n−1) moves. The recursive algorithm achieves exactly this bound, which proves it uses the minimum number of moves.
Advantages, Limitations and Applications
Why It Is Useful
- Teaches recursive problem division in a clear way.
- Shows the purpose of base cases and changing parameters.
- Makes call-stack and time-complexity concepts easier to see.
- Helps students practise dry runs and algorithm design.
What to Keep in Mind
- The number of moves becomes huge for large n.
- Wrong rod order produces incorrect moves.
- Deep recursion uses stack memory.
- The puzzle itself is a learning problem, not a general-purpose data structure.
The useful application is mainly in learning recursive problem-solving. The same smaller-problem thinking also appears in divide-and-conquer algorithms, recursive tree processing and backtracking, although those algorithms have their own steps and complexity.
Common Mistakes Students Make
- Moving a bigger disk onto a smaller disk: This breaks the most important puzzle rule.
- Confusing helper and target: Carefully track the roles of each rod in recursive calls.
- Forgetting the stopping condition: Without the base case, calls do not terminate correctly.
- Writing T(n) = n: Tower of Hanoi takes 2n − 1 moves, not n moves.
- Using O(2n) as auxiliary space: The program that prints moves needs only O(n) extra stack space.
- Memorising seven moves without understanding: Learn the n−1 → biggest → n−1 strategy so you can solve different inputs.
Important Questions with Solved Answers
Source Note: These are original PYQ-style practice questions, not verified questions from a particular university's past question paper.
2 Marks: Define Tower of Hanoi.
Answer: Tower of Hanoi is a three-rod puzzle where disks of different sizes must be moved from a source rod to a destination rod. Only one top disk may be moved at a time, and a larger disk cannot be placed on a smaller disk.
2 Marks: Write the Formula for Minimum Moves.
Answer: For n disks, the minimum number of moves is 2n − 1. For example, 4 disks need 15 moves.
2 Marks: What Is the Base Case?
Answer:
In the program shown here, the base
case is n == 0.
The function returns because there
are no disks left to move.
5 Marks: Explain Tower of Hanoi for Three Disks.
Answer: First move the top two disks from A to B with the help of C. Move disk 3 from A to C. Then move the two smaller disks from B to C using A as helper.
The seven legal moves are A→C, A→B, C→B, A→C, B→A, B→C and A→C. These moves place the full tower on C.
5 Marks: Why Is Recursion Used in Tower of Hanoi?
Answer: The problem for n disks is solved by two smaller problems involving n−1 disks. We first move n−1 disks to the helper, move disk n to the target and then move n−1 disks onto the target. The same function solves the smaller problems until the base case is reached.
10 Marks: Explain the Algorithm and Complexity.
Answer: The algorithm takes n disks and three rods. If n = 0, return. Otherwise, call Hanoi for n−1 disks from source to helper, move the largest disk from source to destination and call Hanoi again for n−1 disks from helper to destination.
Its recurrence is T(n) = 2T(n−1) + 1, which solves to T(n) = 2n − 1. Therefore, time complexity is O(2n) and auxiliary space is O(n) because of the recursive call stack. The complete C program is shown in Section 5.
MCQs and Viva Questions
Q1. What is the minimum number of moves for 3 disks?
Answer: 7. Because 2³ − 1 = 7.
Q2. Which disk can be moved from a rod?
Answer: Only its topmost disk.
Q3. What is the minimum number of moves for 5 disks?
Answer: 31. Because 2⁵ − 1 = 31.
Q4. What is the time complexity of printing every Hanoi move?
Answer: O(2n). The number of moves grows exponentially.
Q5. What is the auxiliary space complexity?
Answer: O(n). There can be n+1 active recursive calls including the base case.
Q6. What happens if source and helper are switched incorrectly?
Answer: The generated moves may be incorrect or may not solve the original puzzle. The rod order must match the smaller subproblem.
One-Minute Revision
3 rods • One top disk at a time • Never big on small • Recursion = move n−1, move biggest, move n−1 • Minimum moves = 2n − 1 • Time = O(2n) • Space = O(n).
Conclusion
Tower of Hanoi becomes easy once you stop thinking about all the moves together. First clear the smaller disks, move the biggest disk and then rebuild the tower on the target rod. That same simple plan works for every valid number of disks.
For your semester exam or viva, practise the two-disk example, write the complete seven-move table for three disks and understand why T(n) = 2T(n−1) + 1. With these basics, both the C program and the complexity analysis become much easier to explain.
