Multiply One BigInteger to Another BigInteger in Java

Samual Sam
Updated on 29-Jun-2020 05:26:40

492 Views

BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.To multiply one BigInteger to another, use the BigInteger multiply() method.First, let us create some objects −BigInteger one, two, three; one = new BigInteger("2"); two = new BigInteger("8");Multiply the above and assign it to the third object −three = one.multiply(two);The following is an example −Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, ... Read More

Subtract One BigInteger from Another BigInteger in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:26:03

168 Views

Use the BigInteger subtract() method in Java to Subtract one BigInteger from another.First, let us create some objects −BigInteger one, two, three; one = new BigInteger("200"); two = new BigInteger("150");Subtract the above and assign it to the third object −three = one.subtract(two);The following is an example −Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, two, three;       one = new BigInteger("200");       two = new BigInteger("150");       three = one.subtract(two);       String res = one + " - " + two + " = " +three;       System.out.println("Subtraction: " +res);    } }OutputSubtraction: 200 - 150 = 50

Divide One BigInteger from Another BigInteger in Java

Samual Sam
Updated on 29-Jun-2020 05:25:27

153 Views

Use the BigInteger divide() method in Java to divide one BigInteger from another.First, let us create some objects.BigInteger one, two, three; one = new BigInteger("200"); two = new BigInteger("100");Divide the above and assign it to the third object −three = one.divide(two);The following is an example −Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, two, three;       one = new BigInteger("200");       two = new BigInteger("100");       // division       three = one.divide(two);       String res = one + " / " + two + " = " +three;       System.out.println("Result: " +res);    } }OutputResult: 200 / 100 = 2

Negate a BigInteger in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:24:37

237 Views

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 Demoimport 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);    } }OutputActual Value: 200 Negated Value: -200

Calculate Power on a BigInteger in Java

Samual Sam
Updated on 29-Jun-2020 05:24:05

304 Views

Use the BigInteger pow() method in Java to calculate the power on a BigInteger.First, let us create some objects.BigInteger one, two; one = new BigInteger("5");Perform the power operation and assign it to the second object −// power operation two = one.pow(3);The following is an example −Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one, two;       one = new BigInteger("5");       System.out.println("Actual Value: " +one);       // power operation       two = one.pow(3);       System.out.println("Result: " +two);    } }OutputActual Value: 5 Negated Value: 125

Perform Bitwise Operations with BigInteger in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:22:38

343 Views

BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.Let us work with the testBit() method in Java to perform Bitwise operation. The java.math.BigInteger.testBit(int n) returns true if and only if the designated bit is set −The following is an example −Example Live Demoimport java.math.*; public class BigIntegerDemo {    public static void main(String[] args) {       BigInteger one;       Boolean two;       one = new BigInteger("5"); ... Read More

Shift Right in BigInteger in Java

Samual Sam
Updated on 29-Jun-2020 05:22:00

223 Views

To shift right in a BigInteger, use the shiftRight() method.The java.math.BigInteger.shiftRight(int n) returns a BigInteger whose value is (this >> n). Sign extension is performed. The shift distance, n, may be negative, in which case this method performs a left shift. It computes floor(this / 2n).The following is an example −Example Live Demoimport java.math.*; public class Demo {    public static void main(String[] args) {       BigInteger one;       one = new BigInteger("25");       one = one.shiftRight(3);       System.out.println("Result: " +one);    } }OutputResult: 3

Enum for Days of Week in Java

Samual Sam
Updated on 29-Jun-2020 05:19:04

2K+ Views

To set enum for days of the week, set them as constantsenum Days { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }Now create objects and set the above constants −Days today = Days.Wednesday; Days holiday = Days.Sunday;The following is an example −Example Live Demopublic class Demo {    enum Days {       Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday    }    public static void main(String[] args) {       Days today = Days.Wednesday;       Days holiday = Days.Sunday;       System.out.println("Today = " + today);       System.out.println(holiday+ " is holiday");    } }OutputToday = Wednesday Sunday is holiday

Equals and Operator for Enum Data Type in Java

Samual Sam
Updated on 29-Jun-2020 05:17:47

217 Views

We have the Devices Enum with four constants.enum Devices { LAPTOP, MOBILE, TABLET, DESKTOP; }We have created some objects and assigned them with the constants.Let us compare them with both equals() and ==. Firstly, begin with equals() −if(d1.equals(d2)) System.out.println("Devices are the same."); else System.out.println("Devices are different.");Now, let us move forward and check for ==if(d1 == d3) System.out.println("Devices are the same."); else System.out.println("Devices are different.");The following is the final example demonstrating both equals() and == operator −Example Live Demopublic class Demo {    enum Devices {       LAPTOP, MOBILE, TABLET, DESKTOP;    }    public static void main(String[] args) { ... Read More

Comparing Enumeration Values in Java

karthikeya Boyini
Updated on 29-Jun-2020 05:17:11

274 Views

To compare enumeration values, use the equals() method.Our Devices enum is having some objects with values assigned to them.Devices d1, d2, d3; d1 = Devices.LAPTOP; d2 = Devices.LAPTOP; d3 = Devices.TABLET;Let us compare them −if(d3.equals(Devices.TABLET)) System.out.println("Devices are same."); else System.out.println("Devices are different.");The following is an example −Example Live Demopublic class Demo {    enum Devices {       LAPTOP, MOBILE, TABLET;    }    public static void main(String[] args) {       Devices d1, d2, d3;       d1 = Devices.LAPTOP;       d2 = Devices.LAPTOP;       d3 = Devices.TABLET;       if(d1.equals(d2))   ... Read More

Advertisements