Sunday, June 27, 2010

Program to solve Tower Of Hanoi problem in C

 TOWER OF HANOI:

 Here is the function that solves the tower of hanoi problem.
  •  2^n-1 moves are required in solving the problem.
  •  The moves are   move n-1 disks  form source to middle,move n disks from source to destination,move n-1 disks from middle to destination.   

Here is the code

void hanoi(int n,int s,int i,int d)
{ if(n>0)
 {
    hanoi(n-1,s,d,i);
    printf("move %d from %d to %d\n",n,s,d);
    hanoi(n-1,i,s,d);
  }
}

No comments: