Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- void Hanoi(int,char,char,char);
- int main(void)
- {
- int disks;
- printf("Enter the number of disks : ");
- scanf("%d",&disks);
- int z = (pow(2,disks))-1;
- printf("Total moves: %d \n",z);
- printf("The sequence of moves:\n");
- Hanoi(disks,'S','I','D');
- return 0;
- }
- void Hanoi(int disk,char source,char intermediate,char destination)
- {
- if(disk==1)
- {
- printf("Move disk 1 from peg %c to peg %c \n",source,destination);
- return;
- }
- Hanoi(disk-1,source,destination,intermediate);
- printf("Move disk %d from peg %c to peg %c . \n",disk,source,destination);
- Hanoi(disk-1,intermediate,source,destination);
- }
Add Comment
Please, Sign In to add comment