F How implement tower of HANOI problem? [using C] | CodeTheta

How implement tower of HANOI problem? [using C]

May 18, 2013


Here you can see it is a Tower of Hanoi problem it is a most popular program in the world. It is also called as a Tower of Brahma or sometimes also called as Lucas Tower.It is a mathematical game or puzzle thing. This theory consist three similar rods and several number of disk.

It follows some rules: Those are Here

/*
   --------------------------------------------
   Program to implement TOWER OF HANOI PROBLEM.
   http://native-code.blogspot.com
   --------------------------------------------
*/

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

void hanoi_tower(char,char,char,int);
int i=0;

void main()
{
int n;
clrscr();
printf("\t\t\t\t ! TOWER OF HANOI PROBLEM !");
printf("\n Input the no. of Disc : ");
scanf("%d",&n);
printf("\n Tower of Hanoi for %d DISC",n);
hanoi_tower('X','Y','Z',n);
getch();
}

void hanoi_tower(char peg1,char peg2,char peg3, int n)
{
if(n<=0)
  printf("\n Illegal Entry");

if(n==1)
  {
  i++;
  printf("\n Step No. -> %d",i);
  printf(" ==> Move Disk from %c to %c",peg1,peg3);
  }
else
  {
  hanoi_tower(peg1,peg3,peg2,n-1);
  hanoi_tower(peg1,peg2,peg3,1);
  hanoi_tower(peg2,peg1,peg3,n-1);
  }
}

Post a Comment