def towerOfHanoi(N , source, destination, auxiliary):
if N==1:
print("Move disk 1 from source",source,"to destination",destination)
return
towerOfHanoi(N-1, source, auxiliary, destination)
print("Move disk",N,"from source",source,"to destination",destination)
towerOfHanoi(N-1, auxiliary, destination, source)
# Driver code
N = 3
towerOfHanoi(N,'A','B','C')
# A, C, B are the name of rods
// C++ recursive function to
// solve tower of hanoi puzzle
#include <bits/stdc++.h>
using namespace std;
void towerOfHanoi(int n, char from_rod, char to_rod,
char aux_rod)
{
if (n == 0) {
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
cout << "Move disk " << n << " from rod " << from_rod
<< " to rod " << to_rod << endl;
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
// Driver code
int main()
{
int N = 3;
// A, B and C are names of rods
towerOfHanoi(N, 'A', 'C', 'B');
return 0;
}
// This is code is contributed by rathbhupendra
# Recursive Python function to solve tower of hanoi
def TowerOfHanoi(n , from_rod, to_rod, aux_rod):
if n == 0:
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)
# Driver code
n = 4
TowerOfHanoi(n, 'A', 'C', 'B')
# A, C, B are the name of rods
# Contributed By Harshit Agrawal