-
Notifications
You must be signed in to change notification settings - Fork 0
/
TowerOfHanoi.c
140 lines (139 loc) · 2.46 KB
/
TowerOfHanoi.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include<stdio.h>
int count=0;
void move(int,char,char);
void disk1(char,char);
void disk2(char,char,char);
void disk3(char,char,char);
void disk4(char,char,char);
void disk5(char,char,char);
void disk6(char,char,char);
void disk7(char,char,char);
void disk8(char,char,char);
void disk9(char,char,char);
void disk10(char,char,char);
int main(void)
{
int n;
printf("\n-----------------------------------------------------------\n");
printf("\n\t\tWELCOME TO TOWER OF HANOI\n");
printf("\n-----------------------------------------------------------\n");
printf("\n\tA\t\tB\t\tC\n");
printf("\nEnter The Number of Disks : ");
scanf("%d",&n);
switch(n)
{
case 1:
disk1('A','C');
break;
case 2:
disk2('A','B','C');
break;
case 3:
disk3('A','B','C');
break;
case 4:
disk4('A','B','C');
break;
case 5:
disk5('A','B','C');
break;
case 6:
disk6('A','B','C');
break;
case 7:
disk7('A','B','C');
break;
case 8:
disk8('A','B','C');
break;
case 9:
disk9('A','B','C');
break;
case 10:
disk10('A','B','C');
break;
default :
printf("\n\tINVALID INPUT\nThe Number of Disks between 1 to 10 ");
}
printf("\n\n-----------------------------------------------------------\n");
printf("\n\t\tTOTAL NO. OF STEPS : %d\n",count);
printf("\n-----------------------------------------------------------\n");
printf("\n\t\t------ THANK YOU -----\n");
printf("\n\t\t\t\t\tBY :- AMAN GUPTA\n");
printf("\n-----------------------------------------------------------\n");
return 0;
}
void disk1(char S,char E)
{
move(1,S,E);
return;
}
void disk2(char S,char M,char E)
{
disk1(S,M);
move(2,S,E);
disk1(M,E);
return;
}
void disk3(char S,char M,char E)
{
disk2(S,E,M);
move(3,S,E);
disk2(M,S,E);
return;
}
void disk4(char S,char M,char E)
{
disk3(S,E,M);
move(4,S,E);
disk3(M,S,E);
return;
}
void disk5(char S,char M,char E)
{
disk4(S,E,M);
move(5,S,E);
disk4(M,S,E);
return;
}
void disk6(char S,char M,char E)
{
disk5(S,E,M);
move(6,S,E);
disk5(M,S,E);
return;
}
void disk7(char S,char M,char E)
{
disk6(S,E,M);
move(7,S,E);
disk6(M,S,E);
return;
}
void disk8(char S,char M,char E)
{
disk7(S,E,M);
move(8,S,E);
disk7(M,S,E);
return;
}
void disk9(char S,char M,char E)
{
disk8(S,E,M);
move(9,S,E);
disk8(M,S,E);
return;
}
void disk10(char S,char M,char E)
{
disk9(S,E,M);
move(10,S,E);
disk9(M,S,E);
return;
}
void move(int N,char S,char E)
{
printf("\nmove %d from %c to %c",N,S,E);
count++;
return;
}