-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnyBaseSubtraction.java
46 lines (35 loc) · 1.08 KB
/
AnyBaseSubtraction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package numberssystem;
import java.util.Scanner;
public class AnyBaseSubtraction {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int base = scanner.nextInt();
int sum = anyBaseSubtraction(num1, num2, base);
System.out.println(sum);
}
private static int anyBaseSubtraction(int num1, int num2, int base) {
int result = 0;
int power = 1;
int carry = 0;
while (num1 > 0) {
int digit1 = num1 % 10;
int digit2 = num2 % 10;
num1 = num1 / 10;
num2 = num2 / 10;
digit1 = digit1 + carry;
int digit = 0;
if (digit1 >= digit2) {
carry = 0;
digit = digit1 - digit2;
} else {
carry = -1;
digit = (digit1 + base) - digit2;
}
result = result + (digit * power);
power = power * 10;
}
return result;
}
}