/**
* Division without using '/' operator
* @author PRATEEK
*/
class Division {
public static int division(int dividend, int divisor) {
if (divisor == 0)
boolean isPositive = true; // flag for negative number
if (dividend < 0) {
isPositive = !isPositive; // toggle flag, i.e.negative
dividend *= -1; // make divident positive
}
if (divisor < 0) {
isPositive = !isPositive;
divisor *= -1;
}
int temp, mul, result = 0;
for (; dividend >= divisor; dividend -= temp >> 1, result += mul >> 1)
for (mul = 1, temp = divisor; temp <= dividend; temp <<= 1, mul <<= 1);
return result;
}
public static void main
(String[] args
) { int numerator = 56;
int denominator = 5;
int ans = division(numerator, denominator);
System.
out.
println(numerator
+"/"+denominator
+"="+ans
); }
}
Ci8qKgogKiBEaXZpc2lvbiB3aXRob3V0IHVzaW5nICcvJyBvcGVyYXRvcgogKiBAYXV0aG9yIFBSQVRFRUsKICovCiBjbGFzcyBEaXZpc2lvbiB7CgoJcHVibGljIHN0YXRpYyBpbnQgZGl2aXNpb24oaW50IGRpdmlkZW5kLCBpbnQgZGl2aXNvcikgewoJCWlmIChkaXZpc29yID09IDApCgkJCXRocm93IG5ldyBBcml0aG1ldGljRXhjZXB0aW9uKCJEaXZpc2lvbiBieSBaZXJvIik7CgoJCWJvb2xlYW4gaXNQb3NpdGl2ZSA9IHRydWU7IC8vIGZsYWcgZm9yIG5lZ2F0aXZlIG51bWJlcgoKCQlpZiAoZGl2aWRlbmQgPCAwKSB7CgkJCWlzUG9zaXRpdmUgPSAhaXNQb3NpdGl2ZTsgLy8gdG9nZ2xlIGZsYWcsIGkuZS5uZWdhdGl2ZQoJCQlkaXZpZGVuZCAqPSAtMTsgLy8gbWFrZSBkaXZpZGVudCBwb3NpdGl2ZQoJCX0KCgkJaWYgKGRpdmlzb3IgPCAwKSB7CgkJCWlzUG9zaXRpdmUgPSAhaXNQb3NpdGl2ZTsKCQkJZGl2aXNvciAqPSAtMTsKCQl9CgoJCWludCB0ZW1wLCBtdWwsIHJlc3VsdCA9IDA7CgkJZm9yICg7IGRpdmlkZW5kID49IGRpdml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