Site icon codippa

How to divide two numbers without using division (/) operator in java / How to find remainder of division of two numbers using minus (-) operator / Java program to find remainder of division using –

Suppose you want to divide a number a by another number b in java, you will use a / b. So simple, isn’t it. But what if you are required to determine the result by using only minus(-) operator, that is, without using division (/) operator.

Terms

Following are the common terms used in division of numbers

Dividend : The number which is divided.
Divisor : The number which divides another number.
Remainder : Number which is left as a result of division.
Quotient : Number which is obtained by dividing one digit with another.

For Example, in the division of 13 is divided by 4 (13/4), 13 is the dividend, 4 is the divisor, 1 is the remainder and 3 is the quotient.

Logic

Division of two numbers is a repeated subtraction of dividend and divisor till the dividend becomes less than the divisor.

In every step,

  1. the divisor is subtracted from the remainder of previous step.
  2. the remainder of the subtraction is treated as the dividend of the next step.

These steps are carried out till the difference of any step(or dividend of next step) becomes less than the divisor. At the end, the digit which is left finally is the remainder and the number of steps carried out is the quotient

It can be observed that in all steps the divisor(or the number which is subtracted) remains the same, only the dividend keeps on changing.

For Example, if 13 is divided by 4, then according to above logic, the division would consist of following steps:

13 – 4 = 9
  9 – 4 = 5
  5 – 4 = 1

Now, the digit which is left finally is 1, which is the remainder and the number of steps carried out is 3, which is the quotient.
Here goes the program.

public static void divide() {
   // initialize dividend and divisor
   int dividend = 13;
   int divisor = 4;
   // initialize quotient
   int quotient = 0;
   //loop till the divisor does not become smaller than dividend
   while(dividend >= divisor)
   {
      dividend = dividend - divisor;
      quotient++;
   }
   // print results
   System.out.println("Quotient is "+quotient);
   // result of last subtraction
   System.out.println("Remainder is "+dividend);
}

Output

Quotient is 3
Remainder is 1

The approach will not work and will give unexpected result in case either divisor or dividend is negative. There is a way out to resolve this problem. Since, in division, we are only concerned with the quotient and remainder and the numeric values of the divisor and dividend. Thus, if your input can contain negative numbers, then before starting the division process, check for negative values and convert them to positive numbers by multiplying them with -1. This will have no effect on the result. Example,

public static void divide() {
   // initialize dividend and divisor
   int dividend = 13;
   int divisor = -4;
   // check for negative dividend
   if(dividend < 0) {
      // convert it to positive value
      dividend = dividend * -1;
   }
   // check for negative divisor
   if(divisor < 0) {
      // convert it to positive value
      divisor = divisor * -1;
   }
   // initialize quotient
   int quotient = 0;
   //loop till the divisor does not become smaller than dividend
   while(dividend >= divisor)
   {
      dividend = dividend - divisor;
      quotient++;
   }
   // print results
   System.out.println("Quotient is "+quotient);
   // result of last subtraction
   System.out.println("Remainder is "+dividend);
}

Output

Quotient is 3
Remainder is 1

See, the output remains the same.

Hope you liked this post. Keep visiting for more !!!

Exit mobile version