In this tutorial post, we will write a Tower of Hanoi Recursive program in the c programming language in which the user will enter the number of disks.
#include<stdio.h>
#include<conio.h>
void tower(int, char, char, char);
void main()
{
int n;
clrscr();
printf("Enter how many disks");
scanf("%d",&n);
tower(n,'A','B','C');
getch();
}
void tower(int n, char beg, char aux, char end)
{
if(n == 1)
{
printf("Disk 1 is transfered from %c to %c\n",beg,end);
return;
}
tower(n-1,beg,end,aux);
printf("Disk %d is transferred from %c to %c\n",n,beg,end);
tower(n-1,aux,beg,end);
}