------------------------------------------------------------------------------
#include<iostream>
#include<stdlib.h>
using namespace std;
void towers(int n,char source,char inter,char destination);
int main()
{
int n;
cout<<"\nEnter the number of disks";
cin>>n;
towers(n,'A','B','C');
return 0;
}
void towers(int n,char source,char inter,char destination)
{
//Moving 1 disk from A to C
if(n==1)
{
cout<<"\nMove disk 1 from A to C";
return ;
}
// move n-1 disks from A to B using C
towers(n-1,source,destination,inter);
cout<<"\nMove disk "<<n<<" from "<<source<<" to "<<destination;
//move n-1 disks from B to C USing A
towers(n-1,inter,source,destination);
}//end of the tower function
----------------------------------------------------------------
output:-
#include<iostream>
#include<stdlib.h>
using namespace std;
void towers(int n,char source,char inter,char destination);
int main()
{
int n;
cout<<"\nEnter the number of disks";
cin>>n;
towers(n,'A','B','C');
return 0;
}
void towers(int n,char source,char inter,char destination)
{
//Moving 1 disk from A to C
if(n==1)
{
cout<<"\nMove disk 1 from A to C";
return ;
}
// move n-1 disks from A to B using C
towers(n-1,source,destination,inter);
cout<<"\nMove disk "<<n<<" from "<<source<<" to "<<destination;
//move n-1 disks from B to C USing A
towers(n-1,inter,source,destination);
}//end of the tower function
----------------------------------------------------------------
output:-
No comments:
Post a Comment