Register | Login

The task is to divide a number by 7 efficiently without using {/, *, -} operators.

Possible Solution:

The solution is to realize that we can do this operation by dividing number on 8 using shift operation X >> 3 = x/8 and utilizing modulus operator %.
The algorithm is to divide number by 8 continually, changing the number to to the sum of division and the remainder.


int Divide(int Number)
{
int value = 0;
while (Number > 7)
int temp = (Number >> 3);
value += temp;
Number = (Number % 8) + temp;
}
if (Number == 7) value++;
return value;
}




Who Voted for this Question


Article



Common Interview is a place to help people keep up with the latest trends in job interviewing. You can interact by asking interview questions or by providing answers and ratings. Choose from thousands behavioural, technical, testing or program management questions and interview puzzles.