What is recursion? Write a recursive procedure that gives a solution to the tower of Hanoi problem for N disks.

What is recursion? Write a recursive procedure that gives a solution to the tower of Hanoi problem for N disks.


Answer:

Recursion:  When a function calls itself then it is recursive function.  Suppose P is a procedure containing either a call to itself to a second procedure that may eventually result in a call statement back to the original procedure P. Then P is called a recursive procedure.


Solution to the tower of Hanoi problem for N disks:

TOWER ( N, BEG, AUX, END )
This procedure gives a recursive solution to the tower of Hanoi problem N disks.

1. If (N=1) then
                                Write: BEG → END  and return.
2. Call TOWER( N-1, BEG, END, AUX)
3. Write: BEG → END.
4. Call Tower (N-1, AUX, BEG, END)

5. Return.

No comments

Dear Members, Thanks for Your Comments. We must be reply your comment answer as soon as possible. Please Stay with us.....

Theme images by ideabug. Powered by Blogger.