Skip to content

Commit e9276ea

Browse files
authored
Merge pull request #7 from imabhishek02/main
towerofhanoi
2 parents f21f511 + ba93b4e commit e9276ea

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Tower_of_hanoi.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// C++ recursive function to
2+
// solve tower of hanoi puzzle
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
void towerOfHanoi(int n, char from_rod, char to_rod,
7+
char aux_rod)
8+
{
9+
if (n == 0) {
10+
return;
11+
}
12+
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
13+
cout << "Move disk " << n << " from rod " << from_rod
14+
<< " to rod " << to_rod << endl;
15+
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
16+
}
17+
18+
// Driver code
19+
int main()
20+
{
21+
int N = 3;
22+
23+
// A, B and C are names of rods
24+
towerOfHanoi(N, 'A', 'C', 'B');
25+
return 0;
26+
}
27+
28+
// This is code is contributed by abhishek rohit

0 commit comments

Comments
 (0)