Skip to content

Commit 04997ff

Browse files
tower of hanoi
1 parent f7cc99c commit 04997ff

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Tower Of Hanoi/TOH.c.txt

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// C++ recursive function to
2+
// solve tower of hanoi puzzle
3+
#include <bits/stdc++.h>
4+
#include<stdio.h>
5+
#include<stdlib.h>
6+
7+
void towerOfHanoi(int n, char from_rod,
8+
char to_rod, char aux_rod)
9+
{
10+
if (n == 1)
11+
{
12+
printf("Move disk 1 from rod %c to %c" ,from_rod,to_rod);
13+
printf("\n");
14+
return;
15+
}
16+
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
17+
printf("Move disk %d from rod %c to rod %c",n,from_rod,to_rod);
18+
printf("\n");
19+
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
20+
}
21+
22+
// Driver code
23+
int main()
24+
{
25+
int n = 4; // Number of disks
26+
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
27+
return 0;
28+
}
29+
30+
31+

0 commit comments

Comments
 (0)