-
Notifications
You must be signed in to change notification settings - Fork 0
/
47_tower_of_hanoi.c
52 lines (47 loc) · 1.42 KB
/
47_tower_of_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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**********************************************************************************************
DOCUMENTATION:
NAME :V.KARTHIEKEYAN
DATE :2.07.2021
DESCRIPTION :WAP implement the solution for tower of hanoi
OUTPUT :./a.out
Enter the number of disks N : 3
Make the legal move from rod A to C
Make the legal move from rod A to B
Make the legal move from rod C to B
Make the legal move from rod A to C
Make the legal move from rod B to A
Make the legal move from rod B to C
Make the legal move from rod A to C
DO YOU WANT TO CONTINUE (y/Y):n
************************************************************************************************/
#include <stdio.h>
#include<stdio_ext.h>
void towers(int, char, char, char);
int main()
{
int num;
char choice;
do
{
__fpurge(stdin);
printf("Enter the number of disks N : ");
scanf("%d", &num);
towers(num, 'A', 'C', 'B'); //from , to , buffer
__fpurge(stdin);
printf("\n\nDO YOU WANT TO CONTINUE (y/Y):");
scanf(" %c",&choice);
}
while(choice=='y' || choice=='Y');
return 0;
}
void towers(int num, char from_rod, char to_rod, char buffer)
{
if (num == 1)
{
printf("\nMake the legal move from rod %c to %c", from_rod, to_rod);
return;
}
towers(num - 1, from_rod, buffer, to_rod);
printf("\nMake the legal move from rod %c to %c", from_rod, to_rod);
towers(num - 1, buffer, to_rod, from_rod);
}