Sunday 27 March 2016

Arithmatic Operation Of BigInteger in JAVA





BigInteger class is used in java for computation of very large Integers. 
It's one of the easiest class to implement and it's functions are also very simple.  

import java.math.*;
class DemoBigInteger{
public static void main(String s1[]){
    BigInteger bi1 = new BigInteger("-1211111111111111111111111");
    BigInteger bi2 = new BigInteger("21212121");
    BigInteger bi3 = new BigInteger("212121");
    BigInteger bia[] = new BigInteger[2];
 
    //count no. of bit in BigInteger Object
    System.out.println(bi1.bitCount());
 
    //It will display absolute(+ve) value of BigInteger
    bi2 = bi1.abs();
    System.out.print(bi3);
 
 
    System.out.println("\n\n\nOperations in BigInteger\n");
    System.out.println("Opr1 = " + bi2);
    System.out.println("Opr12 = " + bi3);
 
    //Returns sum of bi2 and bi3.
    bi1 = bi2.add(bi3);
    System.out.println("Sum :- "+bi1);
 
    //Returns bi2 - bi3.
    bi1 = bi2.subtract(bi3);
    System.out.println("Sub :- " + bi1);
 
    // Returns division of bi2 and bi3.
    bi1 = bi2.divide(bi3);
    System.out.println("Division :- "+bi1);
 
 
   //Returns array of two BigIntegers representing the result of division and remainder of bi2 and bi3.
    bia = bi2.divideAndRemainder(bi3);
    System.out.println("Division = " + bia[0]);
    System.out.println("Carry = " + bia[1]);

    //Returns greatest common divisor of bi2 and bi3
    bi1 = bi2.gcd(bi3);
    System.out.println("GCD = " + bi1);
 
 
    //Returns maximum of bi2 and bi3.
    bi1 = bi2.max(bi3);
    System.out.println("Maximum = " + bi1);
 
    //Returns minimum of bi2 and bi3
    bi1 = bi2.min(bi3);
    System.out.println("Minimum = " + bi1);
 
    //Returns remainder after dividing bi2 by bi3
    bi1 = bi2.mod(bi3);
    System.out.println("Reminer(+ve Only) = " + bi1);
 
 
    //Returns product of bi2 and bi3.
    bi1 = bi2.multiply(bi3);
    System.out.println("Multiplication = " + bi1);
 
    int i;
    //Returns bi2 to the int power.
    bi1 = bi2.pow(23);
    System.out.println("Power = " + bi1);
 
    //Returns remainder of dividing bi2 by bi3. May be negative.
    bi1 =bi2.remainder(bi3);
    System.out.println("Reminer = " + bi1);
 
    //-1 for neg numbers, 0 for zero, and +1 for positive
    i = bi1.signum();
    System.out.println("Sign of BI = " + i);
 
 
}
}

No comments:

Post a Comment

How to install google-chrome in redhat without redhat subscription

Install google-chrome in redhat  Download the .rpm file of chrome https://www.google.com/chrome/thank-you.html?installdataindex=empty&st...