forked from dscgecbsp/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTOH.cpp
More file actions
28 lines (24 loc) · 738 Bytes
/
TOH.cpp
File metadata and controls
28 lines (24 loc) · 738 Bytes
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
/*Tower of Hanoi */
/*
This is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying 2 rules:
(i) Only one disk can be moved at a time. (ii) No bigger disk may be placed on top of a smaller disk.
*/
#include <bits/stdc++.h>
using namespace std;
void towerOfHanoi(int n, char A, char B,char C)
{
if (n>1){
towerOfHanoi(n - 1, A,C,B);
cout << "Move disk " << n << " from rod " << A <<
" to rod " << C << endl;
towerOfHanoi(n - 1,B,A,C);
}
}
int main()
{
int n;
cout<<"Enter the value of disks\n";
cin>>n;
towerOfHanoi(n, 'A', 'C', 'B');
return 0;
}