Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream.h>
- void move(int n,char *s,char *i,char *d)
- {
- if(n>0)
- {
- move(n-1,s,d,i);
- cout<<"Disk "<<n<<" is moved from "<<s<<" to "<<d<<endl;
- move((n-1),i,s,d);
- }
- }
- void main()
- {
- int n;
- cout<<"Enter the number of disks ";
- cin>>n;
- if(n==0)
- cout<<"Invalid entry";
- move(n,"s","i","d");
- }
- Output:
- Enter the number of disks 4
- Disk 1 is moved from s to i
- Disk 2 is moved from s to d
- Disk 1 is moved from i to d
- Disk 3 is moved from s to i
- Disk 1 is moved from d to s
- Disk 2 is moved from d to i
- Disk 1 is moved from s to i
- Disk 4 is moved from s to d
- Disk 1 is moved from i to d
- Disk 2 is moved from i to s
- Disk 1 is moved from d to s
- Disk 3 is moved from i to d
- Disk 1 is moved from s to i
- Disk 2 is moved from s to d
- Disk 1 is moved from i to d
- Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment