-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ep1_Hanoi.c
28 lines (26 loc) · 844 Bytes
/
Ep1_Hanoi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void towerSolution(int, char, char, char);
void towerSolution(int numDisks, char sourcePeg, char destPeg, char sparePeg)
{
/* Case 1: if only 1 Disk exists */
if (numDisks == 1)
{
printf("\nMove disk 1 from peg %c to peg %c", sourcePeg, destPeg);
return;
}
// Case 2: if there is more than 1 disk
towerSolution((numDisks - 1), sourcePeg, sparePeg, destPeg);
// Move remaining disks from A to C
printf("\nMove disk %d from peg %c to peg %c", numDisks, sourcePed, destPeg);
// Move (numDisks - 1) disks from B to C using A as the sparePeg
towerSolution((numDisks - 1), sparePeg, destPeg, sourcePeg);
}
int main()
{
int numDisks;
printf("Please enter the number of disks to use: ");
scanf("%d", &numDisks);
printf("Tower of Hanoi involved the moves :\n\n");
towerSolution(numDisks, 'A', 'B', 'C');
printf("\n");
return 0;
}