# Multiplication and Division without ‘*’ and ‘/’ in Java

Okay, so I feel challenged to solve this problem. The problem is how to make program that can calculate multiplication and division without using ‘*’ and ‘/’ as usual. This is about algorithm and math of course.

At first, I think about multiplication. It can be calculate using increased looping (number 1 is added by itself as much as number 2). Next problem arise. What if number 2 is decimal? Okay, we can separate it into two pieces (before comma and after comma) then combine both result.

For example:

`4 x 2,5 → (4 x 2) + (4 x 0,5)`

Okay, next is division. Division  can be calculated using decreased looping (number 1 is subtracted by number 2, and the result is number of iteration). Same as multiplication, what if the number 2 is decimal? How is the looping? What if the result is not 0 in the end of looping? Well we can multiply the result with 10 then do division again with number 2.

For example:

``` 5 : 2 →  5 - 2 = 3 →  3 - 2 = 1 → end of looping, result is 2.
(1 x 10) : 2 →  10 - 2 = 8 → ... →  2 - 2 = 0 → end of looping, result is 5.
Combine both result: 2 + (5 : 10) → 2 + 0,5 → 2,5```

Okay, I decide to make it recursive as it’s better than use much ‘for’ and ‘if’.

When coding, I found new problem. Data type I use is double, which mean not precision. I need more precision data type. Then I search and found about BigDecimal from here and here. So I decide to combine it in code.

So this is the code:

[code language=”java”]

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;

/**
*
* @author Fachri Hilmi
*/
public class ribet {

public static void main(String[] args) {
double numb1 = 2;
double numb2 = 2.5;

System.out.print(numb1 + " x " + numb2 + " = ");
BigDecimal hasilkali = new BigDecimal(String.valueOf(kali(numb1, numb2)));
System.out.println(hasilkali.setScale(scale(numb1, numb2), RoundingMode.HALF_UP).toString());
System.out.print(numb1 + " : " + numb2 + " = ");
BigDecimal hasilbagi = new BigDecimal(String.valueOf(bagi(numb1, numb2)));
System.out.println(hasilbagi.setScale(scale(numb1, numb2), RoundingMode.HALF_UP).toString());
}

public static double kali(double numb1, double numb2) {
double hasil = 0;
if (numb2 > 0 && numb2 < 1) {
hasil += bagi(kali(numb1, kali(numb2, 10)), 10);
} else {
double numb2a = Math.floor(numb2);
double numb2b = numb2 – numb2a;
for (int i = 1; i <= numb2a; i++) {
hasil = tambah(hasil, numb1);
}
if (numb2b != 0) {
hasil += kali(numb1, numb2b);
}
}
return hasil;
}

public static double bagi(double numb1, double numb2) {
double hasil = 0;
if (numb2 == 0) {
System.out.println("Undefined");
System.exit(0);
} else if (numb1 == 0) {
hasil = 0;
} else if (numb1 < numb2) {
numb1 = kali(numb1, 10);
hasil += Double.parseDouble("0." + String.valueOf(bagi(numb1, numb2)).replace(".", ""));
} else {
while (numb1 >= numb2) {
numb1 = kurang(numb1, numb2);
hasil++;
}
if (numb1 != 0) {
hasil += bagi(numb1, numb2);
}
}
return hasil;
}

public static double tambah(double numb1, double numb2) {
BigDecimal bd1 = new BigDecimal(String.valueOf(numb1));
BigDecimal bd2 = new BigDecimal(String.valueOf(numb2));

BigDecimal hasil = bd1.add(bd2).setScale(scale(numb1, numb2), RoundingMode.HALF_UP);

return hasil.doubleValue();
}

public static double kurang(double numb1, double numb2) {
BigDecimal bd1 = new BigDecimal(String.valueOf(numb1));
BigDecimal bd2 = new BigDecimal(String.valueOf(numb2));

BigDecimal hasil = bd1.subtract(bd2).setScale(scale(numb1, numb2), RoundingMode.HALF_UP);

return hasil.doubleValue();
}

public static int scale(double numb1, double numb2) {
String str1 = Double.toString(numb1);
String str2 = Double.toString(numb2);
int dec1 = Integer.valueOf(str1.substring(str1.indexOf(".") + 1, str1.length()));
int dec2 = Integer.valueOf(str2.substring(str2.indexOf(".") + 1, str2.length()));

int scale;
if (dec1 == 0 && dec2 == 0) {
scale = 0;
} else if (dec1 > dec2) {
scale = String.valueOf(dec1).length();
} else {
scale = String.valueOf(dec2).length();
}

return scale;
}
}

[/code]

The output is: Oh yeah, I found something interesting in BigDecimal. It has own add, subtract, multiplication, and division way, more like a class. So I decide to make second program that is really really simple and short.

Check this out:

[code language=”java”]

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

/**
*
* @author Fachri Hilmi
*/
public class simpel {

public static void main(String[] args) {
double numb1 = 4.82;
double numb2 = 0.2;

BigDecimal bd1 = new BigDecimal(String.valueOf(numb1));
BigDecimal bd2 = new BigDecimal(String.valueOf(numb2));

System.out.print(numb1 + " x " + numb2 + " = ");
BigDecimal hasilkali = bd1.multiply(bd2, MathContext.UNLIMITED);
System.out.println(hasilkali.doubleValue());

System.out.print(numb1 + " : " + numb2 + " = ");
BigDecimal hasilbagi = bd1.divide(bd2, RoundingMode.HALF_UP);
System.out.println(hasilbagi.doubleValue());

}
}

[/code]

The output is: Any question and comment would be appreciated. Thank you! 😀

This site uses Akismet to reduce spam. Learn how your comment data is processed.