Here is the towers of Hanoi program using recursion in C
/* Tower of Hanoi using recursion */
/* Tower of Hanoi using recursion */
#include<iostream.h>
#include<conio.h>
void move(int n,int A,int C,int B)
{
if(n==1)
cout<<"\t Move the upper disc from Stack-" <<A<< " to Stack-"
<<C<< "\n";
else
{
move(n-1,A,B,C);
move(1,A,C,B);
move(n-1,B,C,A);
}
}
main( )
{
clrscr( );
cout<<"\n\t ****TOWERS OF HANOI****\n"<<endl;
cout<<" The Mystery of Towers of Hanoi is as follows : \n"<<endl;
move(4,1,3,2);
cout<<"\n\t *************************************************\n"<<endl;
getch( );
return 0;
}
0 comments