60 429
Assignments Done
98,1%
Successfully Done
In April 2018

Answer to Question #44396 in C for vijayasurya

Question #44396
Towers of Hanoi (Recurssion)?
This is the Program. (I have understood how the game works, but not able to understand and trace out the PROGRAM for it.)
#include<stdio.h>
#include<conio.h>
void towers(int n, char source, char dest, char aux);
void main()
{
int n;
clrscr();
printf("\n Enter the number of disks:");
scanf("%d",&n);
towers(n,'A','C','B');
}
void towers(int n, char source, char dest, char aux)
{
if(n==1)
{
printf("\n Move Disk 1 from peg %c to peg %c",source,dest);
return;
}
towers(n-1,source,aux,dest);
printf("\n Move dist %d from peg %c to peg %c\n",n,source,dest);
towers(n-1,aux,dest,source);
}
Please explain how i should trace the program. Also why is the return statement required inside the IF condition? (If i dont give it, it gives me error.) .
Expert's answer

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions