fork download
  1. //
  2. // main.cpp
  3. // Tower of Hanoi
  4. //
  5. // Created by Himanshu on 05/04/22.
  6. //
  7.  
  8. #include <iostream>
  9.  
  10. void towerOfHanoi (int n, char from, char to, char aux) {
  11. if (n <= 0) {
  12. return;
  13. }
  14.  
  15. towerOfHanoi(n-1, from, aux, to);
  16. printf("%c -> %c\n", from, to);
  17. towerOfHanoi(n-1, aux, to, from);
  18. }
  19.  
  20. int main () {
  21. int n = 3;
  22. towerOfHanoi(n, 'A', 'B', 'C');
  23. return 0;
  24. }
  25.  
Success #stdin #stdout 0.01s 5440KB
stdin
Standard input is empty
stdout
A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B