bishalbiswas

tower of hanoi

Sep 6th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include "stdio.h"
  2.  
  3. void towers(int,char,char,char);
  4.  
  5. void towers(int n,char frompeg,char topeg,char auxpeg)
  6. { /* If only 1 disk, make the move and return */
  7. if(n==1)
  8. { printf("\nMove disk 1 from peg %c to peg %c",frompeg,topeg);
  9. return;
  10. }
  11. /* Move top n-1 disks from A to B, using C as auxiliary */
  12. towers(n-1,frompeg,auxpeg,topeg);
  13. /* Move remaining disks from A to C */
  14. printf("\nMove disk %d from peg %c to peg %c",n,frompeg,topeg);
  15. /* Move n-1 disks from B to C using A as auxiliary */
  16. towers(n-1,auxpeg,topeg,frompeg);
  17. }
  18. main()
  19. { int n;
  20. printf("Enter the number of disks : ");
  21. scanf("%d",&n);
  22. printf("The Tower of Hanoi involves the moves :\n\n");
  23. towers(n,'A','C','B');
  24. return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment