Negate a BigInteger in Java

Java 8Object Oriented ProgrammingProgramming

Use the BigInteger negate() method in Java to negate a BigInteger.

First, let us create an object −

BigInteger one, two;
one = new BigInteger("200");

Negate the above and assign it to the second object −

two = one.negate();

The following is an example −

Example

 Live Demo

import java.math.*;
public class BigIntegerDemo {
   public static void main(String[] args) {
      BigInteger one, two;
      one = new BigInteger("200");
      System.out.println("Actual Value: " +one);
      // negate
      two = one.negate();
      System.out.println("Negated Value: " +two);
   }
}

Output

Actual Value: 200
Negated Value: -200
raja
Published on 20-Dec-2018 06:11:22
Advertisements