You are on page 1of 2

Some priests are given three poles and a stack of n gold disks, each disk a little

smaller than the one beneath it. Their assignment is to transfer all n disks from
one of the 3 pole to another with 2 important constraints. They can move only
one disk at a time, and they can never place a larger disk on top of a smaller
one. Design a recursive program for the above Towers of Hanoi puzzle using
stack.

1: Implement the problem using recursion

2: Implement the problem using recursion and also trace the flow of execution

#include<stdio.h>
void move(int, int, int, int);
void main()
{
int n;
printf("Enter the number of discs: ");
scanf("%d",&n);
move(n, 1, 3, 2);
}
void move(int count, int start, int stop, int temp)
{
if(count>0)
{
move(count-1, start, temp, stop);
printf("move the discs %d from %d to %d\n", count, start, stop);

move(count-1, temp, stop, start);


}
}

You might also like