Each time I run this program it gets stuck at scanf
, despite using fflush
-- except that when the input is "2" it works properly. The compiler does not show any error or warning. I have no clue what is going on, but variable x
seems somehow to be affecting program, despite being in an if
block. When x
is 1 in the if
block, the program works as intended, but the loop is infinite.
void nonrec_bin(int);
#include <stdlib.h>
#include <stdio.h>
int main() {
int number;
printf("Enternumber\n");
fflush(stdin);
scanf("%d", &number);
fflush(stdout);
nonrec_bin(number);
return 0;
}
void nonrec_bin(int num) {
int sum = num, x = 1;
printf("%d", sum);
while (x) {
if (num == 1) {
printf("%d", num);
num = sum - num;
x = 0;
}
num /= 2;
}
printf("%d", num);
for (; num;)
if (num % 2 == 0 && num / 2 < 2) {
printf("%d", num);
num = num / 2;
}
}