Im still a relatively new programmer trying to grasp recursion. I have looked over various online tutorials on how they work and they make it seem easy to understand. But when I stumbled upon this question I have troubles attempting to follow how the program runs.
#include <stdlib.h>
#include <stdio.h>
void hanoi(int n);
int main (int argc, char *argv[]) {
hanoi(2);
}
void hanoi( int n ) {
printf("\n\n--------!n-------: %d\n\n", n);
if ( n > 0 ) {
hanoi ( n - 1 );
printf("\n\n--------#n---------: %d\n\n", n);
hanoi ( n - 1 );
printf("\n\n-------$n---------: %d\n\n", n);
}
}
output
!n: 2
!n: 1
!n: 0
#n: 1
!n: 0
$n: 1
#n: 2
!n: 1
!n: 0
#n: 1
!n: 0
$n: 1
$n: 2
So I was hoping that someone could step through this program and help me understand how recursion really works.
Edit: -Sorry about the photos-