Wednesday, 28 August 2013

Recursion is confusing

Recursion is confusing

Can anybody please explain this program? I want to especially know how the
parameters are passed to the function tower and how the recurssion works.
Thanks in advance and here is the code.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter the no. of disks");
scanf("%d",&n);
tower(n,'S','D','T');
getch();
}
tower(int n,char SOURCE,char DEST,char TEMP)
{
if(n>0)
{
tower(n-1,SOURCE,TEMP,DEST);
printf("\nMove disk %d from %c to %c",n,SOURCE,DEST);
tower(n-1,TEMP,DEST,SOURCE);
}
return;
}

No comments:

Post a Comment