C Program for Tower of Hanoi using recursion

C Program for Tower of Hanoi using recursion:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>

void TOH(int num,char x,char y,char z); //Prototyping

main()
{
   int num;
   printf("Enter number of plates:");
   scanf("%d",&num);

   TOH(num-1,'A','B','C');
}

void TOH(int num,char x,char y,char z)
{
   if (num > 0)
    {
      TOH(num-1,x,z,y);
      printf("\n%c->%c",x,y);
      TOH(num-1,z,y,x);
   }
}

OUTPUT:

Enter number of plates:5

A->C
A->B
C->B
A->C
B->A
B->C
A->C
A->B
C->B
C->A
B->A
C->B
A->C
A->B
C->B

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.