In Tower of Hanoi we have to move all the disc from (assumes you have 3 tower namely A, B, C) tower A to tower C, in the same order as the disc are on tower A, with the help of tower B.
#include <stdio.h>
void swap_towers(int, char, char, char); //prototype for my recursive function.
int main()
{
int num_disk;
printf("Enter the number of disks : ");
scanf("%d", &num_disk);
printf("Movement of disks (i.e form Tower A to Tower B) involved in the Tower of Hanoi are :\n");
swap_towers(num_disk, 'A', 'C', 'B');
return 0;
}
void swap_towers(int num, char from_tower, char to_tower, char ex_tower)
{
if (num == 1) // Condition to stop the recursion.
{
printf("\nMove disk 1 from Tower %c to Tower %c", from_tower, to_tower);
return;
}
swap_towers(num - 1, from_tower, ex_tower, to_tower);
printf("\nMove disk %d from Tower %c to Tower %c", num, from_tower, to_tower);
swap_towers(num - 1, ex_tower, to_tower, from_tower);
}