#include<iostream>
usingnamespacestd;
#include<conio.h>
void move(int n,char *s,char *i,char *d)
// s stands for source tower
// d stands for destination tower
// i stands for intermediate tower
{
if(n>0)
{
move(n-1,s,d,i);
// move n-1 disks from source to intermediate tower
cout<<" disk "<<n<<" is moved from "<<s<<" to "<<d<<endl;
// move the disk from to source to destination
move(n-1,i,s,d);
// move n-1 disks from intermediate to destination
}
}
void main()
{
cout<<"Enter the no. of disks:";
int n;
cin>>n;
move(n,"sourcetower","intermediatetower","destination tower");
getch();
}
OUTPUT:
Enter the no. of disks :3
disk 1 is moved from sourcetower to destination tower
disk 2 is moved from sourcetower to intermediatetower
disk 1 is moved from destination tower to intermediatetower
disk 3 is moved from sourcetower to destination tower
disk 1 is moved from intermediatetower to sourcetower
disk 2 is moved from intermediatetower to destination tower
disk 1 is moved from sourcetower to destination tower
usingnamespacestd;
#include<conio.h>
void move(int n,char *s,char *i,char *d)
// s stands for source tower
// d stands for destination tower
// i stands for intermediate tower
{
if(n>0)
{
move(n-1,s,d,i);
// move n-1 disks from source to intermediate tower
cout<<" disk "<<n<<" is moved from "<<s<<" to "<<d<<endl;
// move the disk from to source to destination
move(n-1,i,s,d);
// move n-1 disks from intermediate to destination
}
}
void main()
{
cout<<"Enter the no. of disks:";
int n;
cin>>n;
move(n,"sourcetower","intermediatetower","destination tower");
getch();
}
OUTPUT:
Enter the no. of disks :3
disk 1 is moved from sourcetower to destination tower
disk 2 is moved from sourcetower to intermediatetower
disk 1 is moved from destination tower to intermediatetower
disk 3 is moved from sourcetower to destination tower
disk 1 is moved from intermediatetower to sourcetower
disk 2 is moved from intermediatetower to destination tower
disk 1 is moved from sourcetower to destination tower
No comments:
Post a Comment