Negate a BigInteger in Java



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
karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements