Reputation: 93
I'm attempting to divide the variable 'c' by 2(stored in 'd'). For some reason that doesn't happen. I'm passing 10 and 2 for input right now.
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
BigInteger a = new BigInteger(sc.next());
BigInteger b = sc.nextBigInteger();
BigInteger d = new BigInteger("2");
System.out.println(d);
BigInteger c = a.subtract(b);
c.divide(d);
System.out.println(c);
a.subtract(c);
System.out.println(a);
System.out.println(c);
}
}
Any help would be appreciated. Thanks in advance!
Upvotes: 0
Views: 69
Reputation: 65859
You are forgetting that BigInteger
is immutable. That means that a.divide(b)
does not change a
it returns the result of the calculation.
You need a = a.divide(b)
or similar.
BigInteger a = new BigInteger(sc.next());
BigInteger b = sc.nextBigInteger();
BigInteger d = new BigInteger("2");
System.out.println(d);
BigInteger c = a.subtract(b);
// HERE!
c = c.divide(d);
System.out.println(c);
// HERE!
a = a.subtract(c);
System.out.println(a);
System.out.println(c);
Upvotes: 4
Reputation: 73568
BigInteger
is immutable. You get the result of c.divide(d);
as the return value, which you throw away in your code.
Upvotes: 3