Skip to content

Instantly share code, notes, and snippets.

@Sustainability4
Created April 13, 2021 06:02
Show Gist options
  • Save Sustainability4/b1c6f8936ba93f558fc51728a1d7cad5 to your computer and use it in GitHub Desktop.
Save Sustainability4/b1c6f8936ba93f558fc51728a1d7cad5 to your computer and use it in GitHub Desktop.
Tower of Hanoi Problem
#include <iostream>
using namespace std;
#include <cmath>
// Change in the given string itself. So no need to return or print the changed string.
void towerOfHanoi(int n, char source, char auxiliary, char destination) {
// Power of Recursion
if (n == 0){
return;
}
towerOfHanoi(n-1, source, destination, auxiliary);
cout<< source << " " << destination << endl ;
towerOfHanoi(n-1, auxiliary, source, destination );
}
int main() {
int n;
cin >> n;
towerOfHanoi(n, 'a', 'b', 'c');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment