TOWER OF HANOI using C
I just wrote a program for the Tower of hanoi problem in C using
recursion. But what stresses is how to manage the complexity of such a
problem when the total no. of disks are like 4,5 and whats the logic in
the two TOH() recursive calls in the method itself. The program is as
follows
#include<stdio.h>
#include<conio.h>
void TOH(int n,char x,char y,char z);
void main() {
int n;
printf("\nEnter number of plates:");
scanf("%d",&n);
TOH(n,'A','B','C');
getch();
}
void TOH(int n,char x,char y,char z) {
if(n>0)
{
TOH(n-1,x,z,y); // Recursive call 1
printf("\n%c -> %c",x,y);
TOH(n-1,z,y,x); // Recursive call 2
}
}
No comments:
Post a Comment