WAP to implement tower of hanoi - Algorithm Design and Analysis Lab file


WAP to implement tower  of hanoii

#include <stdio.h>
#include <stdlib.h>

static long step;
static void Hanoi (int n, int from, int to,int spare)
{
if (n>1) Hanoi (n-1,from,spare,to);
printf ("Step %ld: move #%d %d-->%d\n", ++step, n, from, to);
if (n>1) Hanoi (n-1,spare,to,from);

}
int main (int argc, char **argv)
{
int n;

if (argc==1 || (n= atoi(argv[1]))<=0) n= 5;

step= 0;
Hanoi (n, 1, 2, 3);
return 0;
}
Output :


No comments:

Post a Comment