tower of hanoi using recusrion
#include<iostream>
#include<conio.h>
using namespace std;
void hanoi(int n,int fromTower,
int toTower,int spareTower)
{
if(n>0)
{
hanoi((n-1),fromTower,spareTower,toTower);
cout<<"\t Move the Top Disk from Tower-"<<fromTower
<<" to Tower-"<<toTower<<"\n";
hanoi((n-1),spareTower,toTower,fromTower);
}
}
int main( )
{
int n=0;
cout<<"Please enter the number of disks you want to move \n";
cin>>n;
cout<<"\n\t ************** TOWERS OF HANOI **************\n"<<endl;
cout<<"\t The Mystery of Towers of Hanoi is as follows : \n"<<endl;
hanoi(n,1,3,2);
cout<<"\n\t *************************************************";
getch( );
system("pause");
return 0;
}
#include<conio.h>
using namespace std;
void hanoi(int n,int fromTower,
int toTower,int spareTower)
{
if(n>0)
{
hanoi((n-1),fromTower,spareTower,toTower);
cout<<"\t Move the Top Disk from Tower-"<<fromTower
<<" to Tower-"<<toTower<<"\n";
hanoi((n-1),spareTower,toTower,fromTower);
}
}
int main( )
{
int n=0;
cout<<"Please enter the number of disks you want to move \n";
cin>>n;
cout<<"\n\t ************** TOWERS OF HANOI **************\n"<<endl;
cout<<"\t The Mystery of Towers of Hanoi is as follows : \n"<<endl;
hanoi(n,1,3,2);
cout<<"\n\t *************************************************";
getch( );
system("pause");
return 0;
}
Comments
Post a Comment