/*
 *  recursive function for the classical tower of Hanoi example 
 */ 

#include <stdio.h> 

void tower(char from_peg, char to_peg, char aux_peg, int n); 

int 
main()
{
    int num_disks; 
    
    printf("Enter the number of disks > "); 
    scanf("%d",&num_disks);  

    printf("Instructions for moving %d disks",num_disks); 
    printf("from A_peg to B_peg using C_peg\n");  

    tower('A','B','C',num_disks); 

    return 0; 

} 

/* 
 *  Displays instructions for moving n disks from from_peg to to_peg using
 *  aux_peg as an auxiliary.  Disks are numbered 1 to n (smallest to 
 *  largest). Instructions call for moving one disk at a time and never 
 *  require placing a larger disk on top of a smaller one.
 */
void
tower(char from_peg, char to_peg, char aux_peg, int  n) 
{
    if (n == 1) {
       printf("Move disk 1 from peg %c to peg %c\n", from_peg, to_peg);
    } 
    else 
    {
       tower(from_peg, aux_peg, to_peg, n - 1);
       printf("Move disk %d from peg %c to peg %c\n", n, from_peg, to_peg);
       tower(aux_peg, to_peg, from_peg, n - 1);
    }
}
