Wednesday, 13 June 2012

Program showing tower of HANOI in which moving disks

June 13, 2012 Posted by Knowledge Bite No comments

/* Program showing tower of HANOI*/
#include<iostream>
#include<stdio.h>
#include<conio.h>

int main()
 {
  int num;
  char beg='1';
  char aux='2' ;
  char end='3';
  void tower(int,char,char,char);

  printf("Enter the number of disks=");
  scanf("%d",&num);
  tower(num,beg,aux,end);

  getch();
  return 0;
 }
 void tower(int num,char beg,char aux,char end)
  {
   if(num==1)
    {
     printf("move disk %d to Peg%c",num,end);
    }
   else
   {
    tower(num-1,beg,end,aux);
    printf("\nMove disk %d from %c to peg %c\n",num,beg,end);
    tower(num-1,aux,beg,end);
   }
  }

OUTPUT

0 Comments:

Post a Comment