Skip to content

Commit b4ec278

Browse files
New Commit
1 parent 3568abc commit b4ec278

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

TowerofHanoi.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
3+
// C recursive function to solve tower of hanoi puzzle
4+
void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
5+
{
6+
if (n == 1)
7+
{
8+
printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);
9+
return;
10+
}
11+
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
12+
printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);
13+
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
14+
}
15+
16+
int main()
17+
{
18+
int n; // Number of disks
19+
printf("Enter number of disks: ");
20+
scanf("%d", &n);
21+
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
22+
return 0;
23+
}

TowerofHanoi.exe

40.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)