Friday, 6 November 2015

Recurrsive solution for the towers of hanoi problem.(CODE)


This topic is taught in B.tech 3rd semester in data structures and algorithms. So, here is a program for this problem.

In this program, you simply have to enter the number of discs and the program will generate the moves required for the given number of discs. I will post the algorithm also.

Please give your comments and tell me if there is any problem with the program.
 HAPPY TO HELP YOU..!!

#include<stdio.h>
#include<conio.h>

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

void towers(int n,char frompeg,char topeg,char auxpeg)
{ /* If only 1 disk, make the move and return */
 if(n==1)
   { printf("%c to %c\t",frompeg,topeg);
     return;
   }
 /* Move top n-1 disks from A to B, using C as auxiliary */
 towers(n-1,frompeg,auxpeg,topeg);
 /* Move remaining disks from A to C */
 printf(" %c to %c\t",frompeg,topeg);
 /* Move n-1 disks from B to C using A as auxiliary */
 towers(n-1,auxpeg,topeg,frompeg);
}
void main()
{
 int n;
clrscr();
 printf("Enter the number of disks : ");
 scanf("%d",&n);
// printf("The Tower of Hanoi involves the moves :\n\n");
 towers(n,'A','C','B');
getch();
}




Expanding the blog


Yay.. expanding the blog to PHP, Java, HTML, CSS, Jquery, AJAX, Andriod, JSP.
Feel free to leave comments!