java given number big

‮ual.www‬tturi.com
java given number big

In Java, you can use the java.math.BigInteger class to represent an arbitrary-precision integer. The BigInteger class provides methods for performing arithmetic operations, such as addition, subtraction, multiplication, and division, as well as comparison operations, such as equals and compareTo.

Here is an example of how to use the BigInteger class to perform arithmetic operations in Java:

import java.math.BigInteger;

public class Main {
  public static void main(String[] args) {
    // Create BigInteger instances
    BigInteger a = new BigInteger("12345678901234567890");
    BigInteger b = new BigInteger("98765432109876543210");

    // Perform arithmetic operations
    BigInteger c = a.add(b);  // c = 22222222212222222110
    BigInteger d = a.subtract(b);  // d = -86419753208641975320
    BigInteger e = a.multiply(b);  // e = 121932631112635269000426605653826388
    BigInteger[] f = a.divideAndRemainder(b);  // f = {0, 12345678901234567890}

    // Print the results
    System.out.println("c = " + c);
    System.out.println("d = " + d);
    System.out.println("e = " + e);
    System.out.println("f = " + f[0] + ", " + f[1]);
  }
}

In this example, we create two BigInteger instances representing large integers, and perform various arithmetic operations on them using the add, subtract, multiply, and divideAndRemainder methods. We then print the results of the operations.

Note that the divideAndRemainder method returns an array containing the quotient and remainder of the division.

You can use the BigInteger class to perform arithmetic operations on large integers that may not fit in the primitive integer data types, such as int or long. However, be aware that the BigInteger class may have slower performance and use more memory than the primitive integer types, depending on the size of the numbers being operated on.

Created Time:2017-11-03 00:14:54  Author:lautturi