This blog serves as a place for me where I can occasionally dump bits of content from my brain. Feel free to browse.

C++ – Hanoi Towers Solution

Please don’t ask me how this code works! :)

#include <iostream>
 
using namespace std;
 
void hanoi(int n, char source, char intermediate, char destination) {
 
    if (n > 0) {
        hanoi(n - 1, source, destination, intermediate);
        cout << " - Moving disk " << n << " from " << source << " to " << destination << endl;
        hanoi(n - 1, intermediate, source, destination);
    }
 
}
 
int main() {
    int n;
    cout << "Enter the no. of disks: "; cin >> n;
    hanoi(n, 'A', 'B', 'C');
    return 0;
}


Leave a Reply