#include<stdio.h>
#include<conio.h>
#include<math.h>
void hanoi(int x, char from,char to,char aux)
{

if(x==1)
{
printf("Move Disk From %c to %c\n",from,to);
}
else
{
hanoi(x-1,from,aux,to);
printf("Move Disk From %c to %c\n",from,to);
hanoi(x-1,aux,to,from);
}

}
void main()
{
 int disk;
 int moves;
 clrscr();
 printf("Enter the number of disks you want to play with:");
 scanf("%d",&disk);
 moves=pow(2,disk)-1;
 printf("\nThe No of moves required is=%d \n",moves);
 hanoi(disk,'A','C','B');
 getch();
 }

Dani AI

Generated

Short answer for : those two recursive calls do NOT run concurrently. Recursion in a single-threaded C program is synchronous and nested — the first recursive call runs to completion (including all of its own deeper recursive calls and returns) before control returns to execute the printf that follows, and only after that printf does the second recursive call begin.

Why that happens (call-stack view)

  • Each invocation creates a stack frame holding parameters and local state.
  • The first hanoi(x-1, ...) call pushes new frames until it hits the base case, then each returns in LIFO order back to the caller.
  • Only after the caller resumes and executes the single-disk move (the printf) does it invoke the second hanoi(x-1, ...).

Concrete trace for n = 3 (call/return produces these printed moves)

  1. Move smallest disk A -> C
  2. Move next disk A -> B
  3. Move smallest disk C -> B
  4. Move largest disk A -> C
  5. Move smallest disk B -> A
  6. Move next disk B -> C
  7. Move smallest disk A -> C

Practical tips and cautions

  • Add a "depth" or indent parameter to your debug prints to visualize nesting (increase indent on each recursive call).
  • Recursion depth is O(n) (safe for typical n), but number of moves is exponential (2^n - 1), so runtime/memory for output grows very fast.
  • If true parallelism is desired, you must use threads/processes and coordinate output; that complicates correctness and is rarely useful for this puzzle.
  • As supplied the standard recursive template, consider posting a clean, portable version to the code library as suggested and keep formatting tidy as requested — it helps others follow the call order when learning recursion.

Recommended Answers

All 5 Replies

Please stop posting poorly formated non-standard code for no reason at all. If you insist on spamming the forum with pointless threads, I'll delete them without hesitation.

If you are so inclined to contribute fully-working code snippets, they would be most appreciated in our code snippet library @ www.daniweb.com/code/ - not the forums.

Thanks!

thanks for the program ...its g8 with few errors which can b corrected easily

If you full code in correct format you can add in the code snippets rather posting on the forum
Thanks!!

i am still in confusion in TOH recursive function how line number 13 and 15 executes both of them executes concurrently or what??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.