[ACCEPTED]-How to check if number is divisible in c#?-division

Accepted answer
Score: 16

If after reducing the fraction as much as 14 possible, the denominator can be expressed 13 as a power of 2 multiplied by a power of 12 5, then the decimal representation terminates. Otherwise 11 it repeats indefinitely.

You could test if 10 your division is "good" as follows:

public bool IsGoodDivision(int a, int b)
{
    while (b % 2 == 0) { b /= 2; }
    while (b % 5 == 0) { b /= 5; }
    return a % b == 0;
}

See 9 it working online: ideone

Note that I am passing 8 the numerator and denominator separately 7 to the method. If you do the division first, then 6 pass the result to your method, you lose 5 precision due to floating point representation error.

Also for 4 production code you should check that b != 0 because 3 it is not allowed to divide by 0. Without 2 the check the above code would go into an 1 infinite loop.

Score: 1

I guess it depends on your definition of 2 "good result" or "easy one". But 1 I think what you want is the Modulus Operator.

More Related questions