chore(CPlusPlus): added tower of hanoi in recursion (#394)

pull/400/head
Abhishek 2021-07-28 00:07:27 +05:30 committed by GitHub
parent c72de71551
commit f01e030335
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 0 deletions

View File

@ -102,3 +102,6 @@
2. [Prime Number](Maths/prime-check.cpp) 2. [Prime Number](Maths/prime-check.cpp)
3. [Prime Sieve](Maths/prime-sieve.cpp) 3. [Prime Sieve](Maths/prime-sieve.cpp)
4. [Fibonacci Series](Maths/fibonaccci-series.cpp) 4. [Fibonacci Series](Maths/fibonaccci-series.cpp)
# Recursion
1. [Tower of Hanoi](Recursion/towerofHanoi.cpp)

View File

@ -0,0 +1,22 @@
#include <iostream>
#include <string>
using namespace std;
void towerOfHanoi(int disks,char stand1,char stand2,char stand3)
{
if(disks==1)
{cout<<"Move disk 1 from "<<stand1<<" to "<<stand3<<endl;
return ;
}
towerOfHanoi(disks-1,stand1,stand3,stand2);
cout<<"Move disk "<<disks<<" from "<<stand1<<" to "<<stand3<<endl;
towerOfHanoi(disks-1,stand2,stand1,stand3);
}
int main(int argc, char const *argv[])
{
int n;
cin>>n;
towerOfHanoi(n,'A','B','C');
}