Thursday, November 22, 2012

C PROGRAM FOR TOWERS OF HANOI


/* C PROGRAM FOR  TOWERS OF HANOI */


#include<stdio.h>
#include<conio.h>

void transfer(int t,char,char,char);
void main()
{
      int n;
      clrscr();
      printf("\n\n\t\t TOWERS OF HANOI");
      printf("\n\n\t\t ~~~~~~~~~~~~~~~~");
      printf("\n\n Enter the no of disk:");
      scanf("%d",&n);
      transfer(n,'l','r','c');
      getch();
  }

void transfer(int n,char from,char to,char temp)
{
if(n>0)
{
       transfer(n-1,from,temp,to);
       printf("\n Move disk %d From %c To %c",n,from,to);
       transfer(n-1,temp,to,from);
    }
 }






















OUTPUT:

                 TOWERS OF HANOI

                 ~~~~~~~~~~~~~~~~

 Enter the no of disk:4

 Move disk 1 From l To c
 Move disk 2 From l To r
 Move disk 1 From c To r
 Move disk 3 From l To c
 Move disk 1 From r To l
 Move disk 2 From r To c
 Move disk 1 From l To c
 Move disk 4 From l To r
 Move disk 1 From c To r
 Move disk 2 From c To l
 Move disk 1 From r To l
 Move disk 3 From c To r
 Move disk 1 From l To c
 Move disk 2 From l To r
 Move disk 1 From c To r





1 comment:

  1. it was very helpful to me....
    thank you for posting this. i have also made a c++ program with same concept but it's somewhat different in the recursive definition
    have a look at my code on my blog codingloverlavi.blogspot.com
    :):)

    ReplyDelete