Pages

Thursday, December 10, 2015

Program of TOH

#include<stdio.h>
#include<conio.h>
void toh(int n,char a,char b,char c);
void main()
{
 int n;
 clrscr();
 printf("Enter the number of disks\t");
 scanf("%d",&n);
          toh(n,'A','B','C');
   getch();
 }

 void toh(int n,char a,char b,char c)
  {
    if(n<=0)
     printf("Number of disks can't be zero (or) less than zero");
   else if(n==1)
     printf("Move disk from %c to %c\n",a,c);
     else
      {
        toh(n-1,a,c,b);
        toh(1,a,b,c);
        toh(n-1,b,a,c);
      }

    }

No comments:

Post a Comment