Open DaidalosCheung opened 4 years ago
#include <stdio.h>
int dec_bi (int dec) {
int num;
num = dec % 2;
if (dec > 2) {
dec_bi( dec / 2 );
printf("In the recursion.\n"); //1. When does not work
putchar( num > 0 ? '1':'0' ); //2. Why omit the first digit
}
printf("After recursion\n");
putchar( num > 0 ? '1':'0' );
return 0;
}
int main (void) {
int dec;
printf("Enter an decimal integer: ");
scanf("%d", &dec);
dec_bi( dec );
return 0;
}
/* For decimal number 6: */
/* 1. After the final recursion finish (6 -> 3 -> 1) and dec reach to 1; */
/* 2. In the final round, dec reach to 1, and (dec = 1) cannot go into if statment, so it will jump to Line13, which print after recursion and then putchar */