Question: How to perform Multiplication and Division without using * or / or % operators?
Problem: Write a code which can be used to Multiply or Divide two integer values without using "*" operator and "/" or "%" operators.
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
Answer:
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 above example if we have to get the result of 6 x 3 = ? then logic will be 6+6+6 = 18.
2. Division :
Problem: Write a code which can be used to Multiply or Divide two integer values without using "*" operator and "/" or "%" operators.
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
Answer:
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 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 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:
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);
}
}
Please do comment and share the post with your friends and colleagues. For any query or question, you can also email me at ashu.kumar940@gmail.com.
Other Blogs:
https://agilehelpdoc.blogspot.in/
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:
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);
}
}
Please do comment and share the post with your friends and colleagues. For any query or question, you can also email me at ashu.kumar940@gmail.com.
Other Blogs:
https://agilehelpdoc.blogspot.in/
No comments:
Post a Comment