-3

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;
        }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
game O
  • 1
  • 1
  • 6
    fflush(stdin) isn't correct code. And printf sometimes doesn't actually print until you print a \n – user253751 Aug 04 '22 at 19:19
  • In general - a correct code is the one that is producing the expected result. Any other code is incorrect. – Eugene Sh. Aug 04 '22 at 19:27
  • 2
    Aside:pleasemakeyourcodemorereadablethannum%2==0&&num/2<2, by using a nice amount of whitespace (and punctuation in the first paragraph). – Weather Vane Aug 04 '22 at 19:27
  • 2
    Do not use use `fflush()` on input. always test the return of `scanf()`. Read the manual. – arfneto Aug 04 '22 at 19:34

2 Answers2

3

Probably the issue that is tripping you up is the "for" loop you have within your "nonrec_bin" function. This statement:

for(;num;)

will act like an endless loop unless the value of "num" is zero (false), as the second section of a for loop is a "true/false" test. So in running your code and placing some "printf" statements in to follow what was happening, if any number other than "2" was entered, that "for" loop was effectively an endless loop.

@Una:~/C_Programs/Console/Divisor/bin/Release$ ./Divisor 
Enter number: 2
Initial value is: 2
Calculated value: 1
Value exiting the while loop: 0

@Una:~/C_Programs/Console/Divisor/bin/Release$ ./Divisor 
Enter number: 18
Initial value is: 18
Calculated value: 1
Value exiting the while loop: 8
^C    <---- had to press "Ctrl-C" to break out of the program.

So, you might need to reconsider how you are trying to use that "for" loop test.

NoDakker
  • 3,390
  • 1
  • 10
  • 11
1

This loop ...

    while(x)
    {    
        if(num==1)
        {   
            printf("%d",num);
            num=sum-num;
            x=0;    
        }
        num/=2;
    }

... will not terminate if num is initially less than 1. Otherwise, given the initial values of the variables and the fact that x is not otherwise used, the whole thing can be replaced by

printf("1");
num = num - 1;

This loop ...

    for (; num; )
        if(num%2==0&&num/2<2)
        {
             printf("%d",num);
             num=num/2;
        }

... iterates until num becomes 0, but it modifies num or outputs anything only if the condition num % 2 == 0 && num / 2 < 2 is satisfied. The only non-negative integers that satisfy that condition are 0 and 2. Therefore, if num is initially 2, the body of the if statement is executed exactly once. That reduces num to 1, however, so the loop is infinite unless num is initially zero (in which case it produces no output).

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thank you dear fellow colleagues it helped and sorry for my rude replies i was asking help and people start bashing me. it got me on my nerves indeed it is 'for loop' that is buggy. – game O Aug 04 '22 at 20:34