Towers of Hanoi Program

The Towers of Hanoi problem is a complex example of a recursive algorithm, yet the function itself is quite simple. The goal is to move a set of disks from one peg to another. The disks are on one of three pegs with the smallest disk on the top, and the largest on the bottom. You must move all the disks from one peg to another, but you can only move one disk at a time, you can only store disks on pegs, and you can not put a larger disk on top of a smaller disk.


#include <stdio.h>
#include <stdlib.h>
void SolveTower( int Disks, char FromPeg, char ToPeg, char AuxPeg )
	if( Disks==1 )
		printf("Move from %c to %c\n", FromPeg, ToPeg );
		SolveTower( Disks-1, FromPeg, AuxPeg, ToPeg );
		printf( "Move from %c to %c\n", FromPeg, ToPeg );
		SolveTower( Disks-1, AuxPeg, ToPeg, FromPeg );

void main()
	int Disks;
	printf( "How many disks?\n" );
	scanf(" %d", &Disks );
	SolveTower( Disks, 'A', 'B', 'C' );