Wednesday, May 11, 2011

Towers of Hanoi example in C/C++

Example C implementation of Towers of Hanoi problem.
It's a very simple implementation based on recursion.


[Towers of Hanoi]

#include<stdio.h>

void move(int from, int to)
{
        printf("\nMove from %d to %d", from, to );
}

void hanoi(int n, int from, int by, int to )
{
        if(n == 1)
                move(from, to);
        else
        {
                hanoi(n-1, from, to, by);
                move(from, to);
                hanoi(n-1, by, from, to);
        }
}

void main()
{
        int i=0;
        puts("\nIf you want to quit, enter minus integer. ");
        while(1)
        {
                puts("\nEnter height of HANOI tower-> ");
                scanf("%d", &i);

                if(i <= 0)
                        break;

                hanoi(i, 1, 2, 3);
        }

}