Pregunta de entrevista de Rocket

Write a code to divide two numbers without using the division and modulus operator

Respuestas de entrevistas

Anónimo

10 sept 2012

Write an algorithm that implements a "long division" (http://en.wikipedia.org/wiki/Long_division). That in theory still needs the div and mod operators, but you can always implement that the other way round, if slower, by subtacting the divisor until the (part of the) number you are currently considering is smaller than the divisor.

2

Anónimo

19 ago 2014

Thanks Kanchan for the solution! One thing I noticed is that answer will result in an infinite loop, it also will not account for an equivalent numerator and denominator. For anyone who refers to the above solution I think this will work: // C#: Simple logic : Note: Works only for Integers int division(int numerator, int denominator) { int result = 0; while (numerator >= denominator) { numerator = numerator - denominator; result++; } return result; }

Anónimo

12 feb 2017

The above answer fails as well, because of the fact that you can't divide a positive number by a negative number or vice versa. IE: 5/(-3) would yielf 5 = 5 - (-3) = 8. the numerator will always be greater than the denominator. Plus it does not account for division by . These are all simple fixes.