-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaBigIntegers.java
More file actions
50 lines (40 loc) · 1.23 KB
/
JavaBigIntegers.java
File metadata and controls
50 lines (40 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Challenge: https://www.hackerrank.com/challenges/java-biginteger
*
* Made by Charles M. Chong
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
public class SolutionBigInteger {
public static void main(String[]args){
BigInteger bigMax = new BigInteger("99999999999999999999");
BigInteger bigMin = new BigInteger("0");
BigInteger a;
BigInteger b;
Scanner in = new Scanner(System.in);
try {
a = in.nextBigInteger();
b = in.nextBigInteger();
// Rules for Relational Operators for Big Integers
// For equality, use left.compareTo(right) == 0.
// For less than, use left.compareTo(right) < 0.
// For greater than, use left.compareTo(right) > 0.
if (((a.compareTo(bigMax) == 0 || a.compareTo(bigMax) < 0) &&
(a.compareTo(bigMin) == 0 || a.compareTo(bigMin) > 0)) && ((b.compareTo(bigMax) == 0 || b.compareTo(bigMax) < 0) &&
(b.compareTo(bigMin) == 0 || b.compareTo(bigMin) > 0))){
System.out.println(a.add(b));
System.out.println(a.multiply(b));
}
else {
System.out.println(a.add(b));
System.out.println(a.multiply(b));
//System.out.println("Must enter value less than 20 characters long.");
}
}
catch (Exception e){
System.out.println(e.toString());
}
}
}