Q:

How do I check if a binary number, provided by continuous user input, is divisible by 5 each time a digit is entered?

A user can enter 0 or 1 which gets appended to a starting number which is 0.
Each time the user adds a digit, the program should tell if the whole number is divisible by 5 or not.
Example:

First number is 0.
User enters '1' - complete number is '1' - the answer is no.
User enters '0' - complete number is '10' - the answer is no.
User enters '1' - complete number is '101' - the answer is yes.

I know the answer has something to do with the last remainder and keeping the last remainder, but I can't find the exact calculation or the logic or the mathematics to do it.

A:

In pseudocode:
remainder=0
while true {
    remainder = remainder % 5
    if remainder = 0
        print "divisbile"
    else
        print "nondivisible"

    remainder = (remainder<<1) + inputBit
}

Explanation: if a number is divisible by 5 a multiple of that number is also divisible by 5, so that part is of no interest. Since your expansion can be modeled as a multiplication by two and an addition, this can be applied to your problem. Now all you do is to multiply the remainder and add the input and check if it is divisble.

A:

int old_x = 0,new_x;
    while(1)
    {
    scanf("%d",new_x);
    new_x= ((old_x<<1)|new_x);
    old_x = new_x ;
    new_x = new_x%5;
    if(!new_x)
    {
    printf("divisible by 5)
    old_x = 0;
    }
    else
    printf("not divisible by 5)
}

