Hello,
I am stuck with this problem . How can I modify the code below to give output like this for n = 3 .
Move 1- top disk from 1 to 3
Move 2- top disk from 1 to 2
Move 3- top disk from 3 to 2
Move 4- top disk from 1 to 3
Move 5- top disk from 2 to 1
Move 6- top disk from 2 to 3
Move 7- top disk from 1 to 3
7 Moves in total
Code:
#include<stdio.h>
void move(int n,int needle1, int needle2, int needle3)
{
if (n==1)
printf("Move top disk from %d to %d \n" ,needle1, needle3);
else
{
move(n-1,needle1, needle3, needle2);
printf("Move top disk from %d to %d \n",needle1, needle3);
move(n-1,needle2, needle1, needle3);
}
}
int main(void)
{
int n;
printf("Input the number of disks : ");
scanf("%d",&n);
move(n,1,2,3);
return 0;
}