forked from PratikshaChuryaA/Hactoberfest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tower_of_Hanoi
41 lines (33 loc) · 1015 Bytes
/
Tower_of_Hanoi
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
#include <iostream>
using namespace std;
class TowerOfHanoi {
public:
TowerOfHanoi(int numDisks) {
this->numDisks = numDisks;
}
void solve() {
moveTower(numDisks, 'A', 'C', 'B');
}
private:
int numDisks;
void moveTower(int disks, char source, char destination, char auxiliary) {
if (disks == 1) {
cout << "Move disk 1 from " << source << " to " << destination << endl;
return;
}
moveTower(disks - 1, source, auxiliary, destination);
cout << "Move disk " << disks << " from " << source << " to " << destination << endl;
moveTower(disks - 1, auxiliary, destination, source);
}
};
int main() {
int numDisks;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"Welcome to the Tower of Hanoi problem"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout << "Enter the number of disks: ";
cin >> numDisks;
TowerOfHanoi toh(numDisks);
toh.solve();
return 0;
}