Implement Tower of Hanoi in C++
Last Updated :
03 Jun, 2024
Tower of Hanoi is mathematical puzzle in which we have 3 rods and n disks. First, all the disks are arranged in first rod from larger disk to smaller disk. We have to move all the disk from the first disk to last disk in same arrangement from larger to smaller.
Rules of Tower of Hanoi
There are following rules in Tower of Hanoi:
- Only one disk can be moved at one point of time.
- Smaller disk can be placed above larger disk, but larger disk cannot be placed above smaller disk.

In this article, we will learn how to implement the Tower of Hanoi algorithm in C++.
Tower of Hanoi Implementation in C++
In C++, we can solve the tower of Hanoi puzzle by both recursivley or iteratively. Below is the algorithm for recursive solution of tower of hanoi. For iterative solution, refer to the article - Iterative Tower of Hanoi.
Algorithm
Create a recursive function that takes the four arguments:
- Number of Disks (n)
- Source Rod (s_rod)
- Destination Rod (d_rod)
- Auxiliary Rod (a_rod)
In this function,
- If there's only one disk:
- Print the move for the single disk.
- Return from the function.
- If there are more than one disks:
- Call the
towerOfHanoi
function to move n-1
disks from the source rod to the auxiliary rod using the destination rod. - Print the move for the nth disk from the source rod to the destination rod.
- Call the
towerOfHanoi
function to move n-1
disks from the auxiliary rod to the destination rod using the source rod.
C++ Program to Implement Tower of Hanoi
C++
// C++ program to illustrate how to implement the tower of
// hanoi algorithm
#include <iostream>
using namespace std;
// Create a recursive function for Tower of Hanoi
void towerOfHanoi(int n, char s_rod, char d_rod, char a_rod)
{
// If there's only one disk
if (n == 1) {
// Print the move for the single disk
cout << "Move disk 1 from rod " << s_rod
<< " to rod " << d_rod << endl;
return;
}
// If there are more than one disks
// Call the towerOfHanoi function to move n-1 disks from
// the source rod to the auxiliary rod using the
// destination rod
towerOfHanoi(n - 1, s_rod, a_rod, d_rod);
// Print the move for the nth disk from the source rod
// to the destination rod
cout << "Move disk " << n << " from rod " << s_rod
<< " to rod " << d_rod << endl;
// Call the towerOfHanoi function to move n-1 disks from
// the auxiliary rod to the destination rod using the
// source rod
towerOfHanoi(n - 1, a_rod, d_rod, s_rod);
}
int main()
{
// Number of disks
int n = 3;
// Call the Tower of Hanoi function
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}
Output
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
Time Complexity: O(2n)
Space Complexity: O(n)
Related Articles:
Similar Reads
Implement Heap in C++ A heap is a type of tree data structure where each node is either greater than or equal to or less than or equal to the values of its children. Based on this property, heaps are classified into two types:Min Heap: Each node is less than or equal to the values of its children.Max Heap: Each node is g
7 min read
Implementation of B+ Tree in C A B+ tree is a self-balancing tree data structure that maintains sorted data and allows searching, inserting, and deletion of data in logarithmic time. In B+ trees, the actual data is stored inside the leaf nodes and the internal nodes act as pointers to the leaf nodes. In this article, we will lear
15+ min read
Implementation of Graph in C Graphs are a versatile data structure that can be used to represent various real-world problems, from social networks to transportation systems. In C, graphs are typically represented using arrays for adjacency matrices or linked lists for adjacency lists. This article will introduce the concept of
13 min read
CSES Solutions - Tower of Hanoi The Tower of Hanoi game consists of three stacks (left, middle, and right) and n round disks of different sizes. Initially, the left stack has all the disks, in increasing order of size from top to bottom. The goal is to move all the disks to the right stack using the middle stack. On each move, you
6 min read
B-Tree Implementation in C++ In C++, B-trees are balanced tree data structures that maintain sorted data and allow searches, sequential access, insertions, and deletions in logarithmic time. B-trees are generalizations of binary search trees (BST) in that a node can have more than two children. B-trees are optimized for systems
13 min read