go to previous page   go to home page   go to next page highlighting

Answer:

3

Calculations with BigInteger follow the same rules as ordinary integer math. Integer division produces an integer result.


add and subtract

BigInteger has add, subtract, multiply, and divide. Here is addition and subtraction:

public BigInteger add( BigInteger val )

Add val to the BigInteger that contains the method. The sum is returned in a new BigInteger

public BigInteger subtract( BigInteger val )

Subtract val from the BigInteger that contains the method. The result is returned in a new BigInteger

BigInteger objects are immutable. The above methods create a new object using the values represented by the operand objects. Here is a program:


import java.math.BigInteger;

class AddSubtract
{

  public static void main ( String[] args )
  {
    BigInteger a = new BigInteger( "45" );
    BigInteger b = new BigInteger( "12" );
    BigInteger c, d;
    
    c = a.add( b );
    d = c.subtract( new BigInteger( "20" ) );

    System.out.println("Result: " + d );
  }
}


Result: 37

QUESTION 6:

What do you imagine is the output of the following?

    BigInteger a = new BigInteger( "10000000000" ); ;
    BigInteger b = new BigInteger( "00000000001" ); ;

    System.out.println( a.subtract( b ) );

go to previous page   go to home page   go to next page