QnA | Perform Multiplication and Division without * or / or % operators.

Problem: How to write a program that can be used for Multiplication or Division two integer values without using “*” operator and “/” or “%” operators?

Java Question and Answer.

Example:
Inputs:-
6 and 3 (Multiply)
50 and 10 (Multiply)
18 and 2 (Divide)
39 and 4 (Divide)
Output:-
Multiplication of 6 and 3 = 18
Multiplication of 50 and 10 = 500
Division of 18 and 6:
Quotient 3 and Remainder 0
Division of 39 and 4:
Quotient 9 and Remainder 3

Solution

We can achieve the same by using addition or subtraction as mentioned below:
1. Multiplication : 

Multiplicand X Multiplier = Result
Adding the ‘Multiplicand’ to itself for the value of ‘Multiplier’ times. As in the above example if we have to get the result of 6 x 3 = ? then logic will be 6+6+6 = 18.

2. Division :

Dividend/Divisor = Quotient with Remainder   

Subtract ‘Divisor’ from ‘Dividend’ along with a counter until the value of dividend gets greater than or equal to the divisor. As in the above example if we have to get the result of 18 / 6 = ? then logic will be
18 – 6 = 12
12 – 6 = 6  
6 – 6 = 0    
As there are three iterations and the end result is zero, the answer will be 18 / 6 = Quotient 3 with remainder 0.

Code Example

public class DivisionMultiplication {

	public static void main(String[] args)
	{
		multiplication(6, 3);
		division(39, 4);
	}

	public static void multiplication(int multiplicand, int multiplier)
	{
// Step #1: Initialize a result variable with 0
		int result = 0;

// Step #2: Saving value of 'multiplier' into new variable 'initialMultiplier'
		int initialMultiplier = multiplier;

// Step #3: Adding the 'multiplicand' in 'result' variable until 'multiplier' is 0.
		while (multiplier > 0)
		{
			result += multiplicand;
			multiplier--;
		}

// Step #4: Use 'format()' of 'String' class to print the result
		String output = String.format("Multiplication of %s and %s = %s", multiplicand, initialMultiplier, result);
		System.out.println(output);
	}

	public static void division(int dividend, int divisor)
	{
// Step #1: Initialize two variables 'result' and 'remainder' with 0.
		int quotient = 0;
		int remainder = 0;

// Step #2: Saving value of 'dividend' into new variable 'initialDividend'
		int initialDividend = dividend;

// Step #3: Now subtract 'divisor' from 'dividend' until, value gets greater than or equal to divisor.
		while (dividend >= divisor)
		{
			remainder = dividend - divisor;

			dividend = remainder;

			quotient++;
		}

// Step #4: Use 'format()' of 'String' class to print the result
		String output = String.format("Division of %s and %s =>", initialDividend, divisor);
		System.out.println(output);
		System.out.printf(" Quotient %s \n Remainder %s", quotient, remainder);

	}
}

2916total visits,4visits today

1 thought on “QnA | Perform Multiplication and Division without * or / or % operators.”

Leave a Reply

Your email address will not be published. Required fields are marked *