C++ Program - Tower Of Hanoi

Implementation of Tower Of Hanoi Problem using C++

 

#include<iostream.h>

#include<conio.h>

 

void towers(int,char,char,char);

 

void main()

{

int n; //Declare the variables to be used

clrscr();

//Get the input for number of disks

cout<<"enter the no of disks : ";

cin>>n;

towers(n,'A','C','B'); //Call the function

getch();

}

 

void towers(int n,char from,char to,char aux)

{

if(n==1) // If there is only one disk

{

cout<<endl<<"move 1 from peg "<<from<<" to "<<to;

return;

}

towers(n-1,from,aux,to); //Recursive Call

cout<<endl<<"move "<<n<<" from peg "<<from<<" to "<<to;

towers(n-1,aux,to,from);

}

 


Dear Guest,
Spend a minute to Register in a few simple steps, for complete access to the Social Learning Platform with Community Learning Features and Learning Resources.
If you are part of the Learning Community already, Login now!
0
Your rating: None

Posted by



Wed, 05/20/2009 - 11:40

Share

Collaborate