Tower of Hanoi Problem in C language


SUBMITTED BY: Neutrino

DATE: Dec. 10, 2020, 3:41 a.m.

FORMAT: Text only

SIZE: 550 Bytes

HITS: 487

  1. #include <stdio.h>
  2. // C recursive function to solve tower of hanoi puzzle
  3. void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
  4. {
  5. if (n == 1)
  6. {
  7. printf("\nMove disk 1 from rod %c to rod %c", from_rod, to_rod);
  8. return;
  9. }
  10. towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
  11. printf("\nMove disk %d from rod %c to rod %c", n, from_rod, to_rod);
  12. towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
  13. }
  14. int main()
  15. {
  16. int n = 5; // Number of disks
  17. towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
  18. return 0;
  19. }

comments powered by Disqus