Md_Touhid

Tower of Hanoi (Recursive)

Jan 27th, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3.  
  4. void hanoi(int, char, char, char);
  5.  
  6. int move_count;
  7. int disks;
  8.  
  9. int main()
  10. {
  11.     printf("Enter the number of disks : ");
  12.     scanf("%d",&disks);
  13.  
  14.     move_count = pow(2,disks)-1;
  15.     printf("\nTotal moves: %d\n\n",move_count);
  16.  
  17.     printf("Moves to be made :\n\n");
  18.  
  19.     hanoi(disks, 'S', 'I', 'D');
  20.  
  21.     return 0;
  22. }
  23.  
  24. void hanoi(int disk_number, char s, char i, char d)
  25. {
  26.     if(disk_number == 1)
  27.     {
  28.         printf("Move disk from %c to %c\n", s, d);
  29.     }
  30.  
  31.     else
  32.     {
  33.         hanoi(disk_number-1, s, d, i);
  34.         printf("Move disk from %c to %c\n", s, d);
  35.  
  36.         hanoi(disk_number-1, i, s, d);
  37.     }
  38. }
Add Comment
Please, Sign In to add comment