fork download
  1.  
  2. /**
  3.  * Division without using '/' operator
  4.  * @author PRATEEK
  5.  */
  6. class Division {
  7.  
  8. public static int division(int dividend, int divisor) {
  9. if (divisor == 0)
  10. throw new ArithmeticException("Division by Zero");
  11.  
  12. boolean isPositive = true; // flag for negative number
  13.  
  14. if (dividend < 0) {
  15. isPositive = !isPositive; // toggle flag, i.e.negative
  16. dividend *= -1; // make divident positive
  17. }
  18.  
  19. if (divisor < 0) {
  20. isPositive = !isPositive;
  21. divisor *= -1;
  22. }
  23.  
  24. int temp, mul, result = 0;
  25. for (; dividend >= divisor; dividend -= temp >> 1, result += mul >> 1)
  26. for (mul = 1, temp = divisor; temp <= dividend; temp <<= 1, mul <<= 1);
  27.  
  28. return result;
  29. }
  30.  
  31. public static void main(String[] args) {
  32. int numerator = 56;
  33. int denominator = 5;
  34. int ans = division(numerator, denominator);
  35. System.out.println(numerator+"/"+denominator+"="+ans);
  36. }
  37. }
  38.  
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
56/5=11