Data Structures and Algorithm
- Runtime
- java 21.0.12
- Files
- 22 files · 16 KB
- Runs
- 50 runs
- Published
package numberSystem;public class AnyBaseSubtraction { public static void main(String[] args) { int n1 = 256; int n2 = 1212; int b = 8; int rv = anyBaseSubtraction(n1, n2, b); System.out.println(rv); } private static int anyBaseSubtraction(int n1, int n2, int b) { int rv = 0; int c = 0; int p = 1; while (n2 != 0) { int d1 = n1 % 10; n1 = n1/10; int d2 = n2 % 10; n2 = n2/10; int d = 0; d2 = d2 + c; if (d2 >= d1) { c = 0; d = d2 - d1; } else { c = -1; d = (d2 + 8) - d1; } rv += d * p; p = p*10; } return rv; }}