DEV Community

loading...

Discussion on: Daily Challenge #143 - Big Arithmetic

Collapse
cssrajput profile image
Chandra Shekhar

Java :)

import java.math.BigInteger;

public class BigArithmetic {

public static void main(String[] args) {
    System.out.println(bigAdd(1, "123456789012345678901234567890"));
    System.out.println(bigAdd(-1, "123456789012345678901234567890"));
    System.out.println(bigSub("123456789012345678901234567890", 1));
    System.out.println(bigSub("123456789012345678901234567890", -1));
}

private static String bigSub(Object a, Object b) {
    return new BigInteger(a.toString()).add(new BigInteger(b.toString())).toString();
}

private static String bigAdd(Object a, Object b) {
    return new BigInteger(a.toString()).add(new BigInteger(b.toString())).toString();
}

}