#include<stdio.h>
#include<conio.h>
void tow(int n,char p1,char p2,char p3)
{
if(n<=0)
printf("ILLEGAL OPERATION");
else
if(n==1)
printf("\n Move disks from %c to %c \n",p1,p3);
else
{
tow(n-1,p1,p3,p2);
tow(1,p1,p2,p3);
tow(n-1,p2,p1,p3);
}
}
void main()
{
int n;
clrscr();
printf("Enter the no of disk \n");
scanf("%d",&n);
tow(n,'a','b','c');
getch();
}
Output:
Enter the number of disk :
2
move disk from a to c
move disk from a to b
#include<conio.h>
void tow(int n,char p1,char p2,char p3)
{
if(n<=0)
printf("ILLEGAL OPERATION");
else
if(n==1)
printf("\n Move disks from %c to %c \n",p1,p3);
else
{
tow(n-1,p1,p3,p2);
tow(1,p1,p2,p3);
tow(n-1,p2,p1,p3);
}
}
void main()
{
int n;
clrscr();
printf("Enter the no of disk \n");
scanf("%d",&n);
tow(n,'a','b','c');
getch();
}
Output:
Enter the number of disk :
2
move disk from a to c
move disk from a to b
No comments:
Post a Comment