Print modulo of BigInteger in Java | Convert large String to BigInteger in Java
Sometimes, as developer we need to divide large number that is represented as String. For divide and get modulo we require value as number (int, long or float). But int, long have some limitations.
So we can use BigInteger class for storing large numbers.
BigInteger class is used for the mathematical operation which involves very big integer calculations that are outside the limit of all available primitive data types.
Example : Check Odd and Even for large number using BigInteger class
public class BigIntegerDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter large number");
BigInteger number = sc.nextBigInteger();
// Getting modulo of entered number
BigInteger ans = number.mod(new BigInteger("2"));
// Check modulo is 0 or not and print based on that
if (ans.equals(new BigInteger("0"))) {
System.out.println(number +" is Even number");
} else {
System.out.println(number +" is Odd number");
}
}
}
Output :
Enter large number
123456789123456789
123456789123456789 is Odd number
Enter large number
98765432112345678901234567890
98765432112345678901234567890 is Even number
Other Java articles :
Merging two sorted Linked List using Recursion approach with Stack trace
Comments
Post a Comment