upsidedown

Tower of hanoi

Feb 2nd, 2012
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include<iostream.h>
  2. void move(int n,char *s,char *i,char *d)
  3. {
  4.     if(n>0)
  5.     {
  6.         move(n-1,s,d,i);
  7.         cout<<"Disk "<<n<<" is moved from "<<s<<" to "<<d<<endl;
  8.         move((n-1),i,s,d);
  9.     }
  10. }
  11. void main()
  12. {
  13.     int n;
  14.     cout<<"Enter the number of disks ";
  15.     cin>>n;
  16.     if(n==0)
  17.         cout<<"Invalid entry";
  18.     move(n,"s","i","d");
  19. }
  20.  
  21. Output:
  22. Enter the number of disks 4
  23. Disk 1 is moved from s to i
  24. Disk 2 is moved from s to d
  25. Disk 1 is moved from i to d
  26. Disk 3 is moved from s to i
  27. Disk 1 is moved from d to s
  28. Disk 2 is moved from d to i
  29. Disk 1 is moved from s to i
  30. Disk 4 is moved from s to d
  31. Disk 1 is moved from i to d
  32. Disk 2 is moved from i to s
  33. Disk 1 is moved from d to s
  34. Disk 3 is moved from i to d
  35. Disk 1 is moved from s to i
  36. Disk 2 is moved from s to d
  37. Disk 1 is moved from i to d
  38. Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment