luc051.c (855B)
1 /* Tower of Hanoi: Write a program to print out the sequence in which 4 disks should be moved from peg A to peg C using peg B. 2 */ 3 /* Let Us C, Chap- 10 (Recursive), Qn No.: B(c) */ 4 /* This file is auto-generated by a bot. */ 5 /* This code is not compiled; it is for reference only. */ 6 7 8 #include <stdio.h> 9 #include <math.h> 10 #include <stdlib.h> 11 12 void hanoi(int, char, char, char); 13 14 int main() 15 { 16 int n = 4; 17 18 printf("Sequence of moves for %d disks:\n\n", n); 19 hanoi(n, 'A', 'B', 'C'); 20 21 return 0; 22 } 23 24 void hanoi(int n, char from_rod, char aux_rod, char to_rod) 25 { 26 if (n == 1) 27 { 28 printf("Move disk 1 from rod %c to rod %c\n", from_rod, to_rod); 29 return; 30 } 31 hanoi(n - 1, from_rod, to_rod, aux_rod); 32 printf("Move disk %d from rod %c to rod %c\n", n, from_rod, to_rod); 33 hanoi(n - 1, aux_rod, from_rod, to_rod); 34 }