149. C Program to solve towers Hanoi problem using recursion.
How does this program work?
- In this C programme you will learn about how to solve towers hanio problem using recursion.
Here is the code
#include <stdio.h>
void creat(int n, char fr, char tr, char ar)
//fr=from rod,tr =to rod, ar=aux rod
{
// program for Tower of Hanoi using Recursive function
if (n == 1)
{
printf("\n Move disk 1 from rod %c to rod %c", fr, tr);
return;
}
creat(n-1, fr, ar, tr);
printf("\n Move disk %d from rod %c to rod %c", n, fr, tr);
creat(n-1, ar, tr, fr); }
int main()
{
int n = 4; // Number of disks are initialized by n
creat(n, 'A', 'B', 'C');
// A, B and C are the name of rod
return 0;
}