HONOI TOWERS USING C
To write a c program to display the honoi towers using recursion function.
/*HONOI TOWERS*/
#include<stdio.h>
#include<conio.h>
void main()
{
void honoi(int,char,char,char);
int n;
clrscr();
printf("\n\nHONOI PROBLEM USING RECURSING");
printf("\nEnter the number of disks:");
scanf("%d",&n);
honoi(n,'L','R','C');
getch();
}
void honoi(int n,char from,char to,char temp)
{
if(n>0)
{
honoi(n-1,from,temp,to);
printf("\n\nMOVE DISKS %d FROM %c TO %c\n",n,from,to);
honoi(n-1,temp,to,from);
}
}
HONOI PROBLEM USING RECURSING
Enter the number of disks: 3
MOVE DISKS 1 FROM L TO R
MOVE DISKS 2 FROM L TO C
MOVE DISKS 1 FROM R TO C
MOVE DISKS 3 FROM L TO R
MOVE DISKS 1 FROM C TO L
MOVE DISKS 2 FROM C TO R
MOVE DISKS 1 FROM L TO R
Comments
Post a Comment