C Program
#include <stdio.h> void towerOfHanoi(int n, char from, char to, char aux) { if (n == 0) return; towerOfHanoi(n - 1, from, aux, to); printf("Move disk %d from %c to %c\n", n, from, to); towerOfHanoi(n - 1, aux, to, from); } int main() { int n = 3; towerOfHanoi(n, 'A', 'C', 'B'); return 0; }
C Output
Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 1 from A to C
C++ Program
#include <iostream> using namespace std; void towerOfHanoi(int n, char from, char to, char aux) { if (n == 0) return; towerOfHanoi(n - 1, from, aux, to); cout << "Move disk " << n << " from " << from << " to " << to << endl; towerOfHanoi(n - 1, aux, to, from); } int main() { int n = 3; towerOfHanoi(n, 'A', 'C', 'B'); return 0; }
C++ Output
Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 1 from A to C
JAVA Program
public class Main { static void towerOfHanoi(int n, char from, char to, char aux) { if (n == 0) return; towerOfHanoi(n - 1, from, aux, to); System.out.println("Move disk " + n + " from " + from + " to " + to); towerOfHanoi(n - 1, aux, to, from); } public static void main(String[] args) { int n = 3; towerOfHanoi(n, 'A', 'C', 'B'); } }
JAVA Output
Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 1 from A to C
Python Program
def tower_of_hanoi(n, from_rod, to_rod, aux_rod): if n == 0: return tower_of_hanoi(n - 1, from_rod, aux_rod, to_rod) print(f"Move disk {n} from {from_rod} to {to_rod}") tower_of_hanoi(n - 1, aux_rod, to_rod, from_rod) n = 3 tower_of_hanoi(n, 'A', 'C', 'B')
Python Output
Move disk 1 from A to C Move disk 2 from A to B Move disk 1 from C to B Move disk 3 from A to C Move disk 1 from B to A Move disk 2 from B to C Move disk 1 from A to C
Explanation
Example
Tower of Hanoi is a famous puzzle in which you are provided with three rods and a pile of disks with varying sizes. The puzzle begins when all the disks are placed on a single rod, decreasing in size from bottom to top. The objective is to transfer the entire stack to a third rod, using two rules: just one disk at a time can be moved, and no disk bigger than another can go on top of it. Recursion is especially suited because n disks are moved essentially exactly like n-1 disks are moved twice with one move in between.
Real-Life Analogy
Suppose you are shifting a stack of books from one shelf to another but, due to some limitation, you can put only a smaller book over a bigger one. You cannot directly shift the largest book and have extra steps for smaller ones; you must shift them first to a separate shelf, then shift the larger book, and at last re-stack the smaller ones over it in the correct order. This "push some things out of the way, do the top-level thing, restore things" pattern is precisely how recursion operates here.
Why It Matters
Tower of Hanoi is not merely a puzzle—it's a well-organized example of recursive problem-solving. It illustrates how a big problem can be divided into a series of little similar problems. The pattern it uses is ubiquitous in programming situations such as file system traversal, sorting algorithms, and divide-and-conquer techniques.
Learning Insights
When you examine the recursive function, you notice that it has three components: move n-1 disks from source to auxiliary rod, move the big disk to destination, and move the n-1 disks from auxiliary rod to destination. Knowing how these naturally interlock makes you visualize recursion as a series of smaller, similar tasks instead of something vague.
Use in Interviews and Projects
In programming interviews, Tower of Hanoi tests your ability to visualize recursion and understand base cases. It’s also a stepping stone for writing recursive solutions to real problems like dependency resolution, undo/redo systems, and network routing algorithms. In project terms, the logic of "temporarily move things aside to make a main change" applies to many scenarios, such as upgrading systems without downtime or rearranging resources.
SEO-Friendly Closing
The Tower of Hanoi recursive solution in C, C++, Java, and Python is arguably one of the best-known examples used for teaching problem decomposition into smaller sub-problems. By learning it, new programmers have a good grasp of base cases, recursive calls, and problem decomposition. Such an understanding can be applied directly in actual coding, competitive programming, and technical interviews where recursion-based problem-solving becomes a necessary skill.
Social Plugin