Microsoft Interview Question

Divide two numbers without using / or % and return quotient.

Interview Answers

Anonymous

Mar 9, 2016

You have a counter and then subtract the divisor from the dividend and add one to the counter. You repeat this until subtracting gives you a negative number. The quotient is whatever your counter is up to and the remainder is whatever the dividend is up to. 16/3: 16-3=13 | counter = 1 13-3=10 | counter = 2 10-3=7 | counter = 3 7-3=4 | counter = 4 4-3=1 | counter = 5 1-3=-2 | counter = 5 (number is too low so we don't increment counter. Instead the quotient is 5 and the remainder is 1)

4

Anonymous

Sep 12, 2017

Version in Java public class Divide { String divide(int a, int b) { if(b !=0) { int counter = 0; while (a >= b) { a = a - b; counter++; } return Integer.toString(counter); }else return "Can't divide by zero"; } }

Anonymous

Dec 27, 2017

int DivideWithoutUsingDivision(int x, int y) { int count = 0; int remainder = 0; while (x >= y) { x -= y; remainder = x; count++; } return remainder; // quotient }

Anonymous

Feb 24, 2016

I answered by counting number of multiples required but the interviewer said that I had to implement his way which was by subtraction and that my way was incorrect.

Anonymous

May 8, 2016

""" Python code : using subtraction """ a=31 b=5 quotient=0 while (a>=b): a=a-b quotient +=1 print "Quotient is : %d"%quotient print "Remainder is : %d"%a