fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void Tower_Of_Hanoi(int N, char A, char C, char B)
  5. {
  6. if (N == 1)
  7. {
  8. cout<<"Move "<<N<<" from "<<A<<" to "<<C<<endl;
  9. return;
  10. }
  11. Tower_Of_Hanoi(N-1,A,B,C);
  12. cout<<"Move "<<N<<" from "<<A<<" to "<<C<<endl;
  13. Tower_Of_Hanoi(N-1,B,C,A);
  14. }
  15.  
  16.  
  17. int main()
  18. {
  19. int N;
  20. cin>>N;
  21. Tower_Of_Hanoi(N,'A','C','B');
  22.  
  23. return 0;
  24. }
Success #stdin #stdout 0s 5284KB
stdin
3
stdout
Move 1 from A to C
Move 2 from A to B
Move 1 from C to B
Move 3 from A to C
Move 1 from B to A
Move 2 from B to C
Move 1 from A to C