1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Tower of Hannoi

Discussion in 'C' started by saomcol, Nov 23, 2006.

  1. saomcol

    saomcol New Member

    Joined:
    Nov 23, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    How can I write a prog to implement the Tower of Hannoi
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void diskchange(char l,char r,char c,int n)
    {
    	if(n>0)
    	{
    		diskchange(l,c,r,n-1);
    		printf("\nMove %d from %c to %c\n",n,l,r);
    		diskchange(c,r,l,n-1);
    	}
    }
    
    int main()
    {
    	int n;
    	printf("\nEnter the number of disks\n");
    	scanf("%d",&n);
    
    	diskchange('L','R','C',n);
    	printf("\nPress any key to exit\n");
    	getch();
    
    	return 0;
    }
    
     
  3. sharma_atul13

    sharma_atul13 New Member

    Joined:
    Jul 16, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    I keenly want to know wat is this tower of hannoi???
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    The Tower of Hanoi is a mathematical game or puzzle which consists of three (n) pegs, and a number of discs of different sizes which can slot onto any peg. The puzzle starts with the discs neatly stacked in order of size on one peg, smallest at the top and moving them to the other peg using the third peg. Condition is at no move you will have the larger peg over the smaller one.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Good sample at wikipedia
    [​IMG]
     

Share This Page