Quicken Loans Interview Question: Write a code to divide two nu... | Glassdoor.co.in

Interview Question

.NET Developer Interview Detroit, MI (US)

Write a code to divide two numbers without using the

  division and modulus operator
Answer

Interview Answer

4 Answers

2

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.

programmer squirrel on 09-Sep-2012
0

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;
        }

PrototypeA on 19-Aug-2014
0

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.

Anon on 12-Feb-2017

One or more comments have been removed.
Please see our Community Guidelines or Terms of Service for more information.

Add Answers or Comments

To comment on this, Sign In or Sign Up.