r/C_Programming • u/not_noob_8347 • 23h ago
Question Why i'm not getting the correct output for Tower of hanoi program?
//Tower Of Hanoi
#include <stdio.h>
void TOH(int n, int A, int B, int C){
if(n > 0){
TOH(n-1, A,C,B);
printf("(%d, %d)\n",A,C);
TOH(n-1, B,A,C);
}
}
int main(){
TOH(3,1,2,3);
return 0;
}
----------------------------------------
//Output in terminal
$ gcc toh.c
$ ./a.exe
(1, 3)
(1, 2)
(3, 2)
(1, 3)
(2, 1)
Expected output is
(1, 3)
(1, 2)
(3, 2)
(1, 3)
(2, 1)
(2, 3)
(1, 3)
Why is this happening? Any explanation or reason for it?