Skip to toolbar

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);

	}
}

One 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 *

Site Statistics

  • Users online: 0 
  • Visitors today : 3
  • Page views today : 3
  • Total visitors : 49,317
  • Total page view: 67,888

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.