rootUser

tower of hanoi recursion

May 26th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<math.h>
  3. void Hanoi(int,char,char,char);
  4. int main(void)
  5. {
  6.     int disks;
  7.     printf("Enter the number of disks : ");
  8.     scanf("%d",&disks);
  9.     int z = (pow(2,disks))-1;
  10.     printf("Total moves: %d \n",z);
  11.     printf("The sequence of moves:\n");
  12.     Hanoi(disks,'S','I','D');
  13.     return 0;
  14. }
  15. void Hanoi(int disk,char source,char intermediate,char destination)
  16. {
  17.     if(disk==1)
  18.     {
  19.         printf("Move disk 1 from peg %c to peg %c \n",source,destination);
  20.         return;
  21.     }
  22.     Hanoi(disk-1,source,destination,intermediate);
  23.     printf("Move disk %d from peg %c to peg %c . \n",disk,source,destination);
  24.     Hanoi(disk-1,intermediate,source,destination);
  25. }
Add Comment
Please, Sign In to add comment