Hello Programmers, In this post, you will know how to solve the Java BigInteger HackerRank Solution. This problem is a part of the HackerRank Java Programming Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
Java BigInteger HackerRank Solutions
Problem
In this problem, you have to add and multiply huge numbers! These numbers are so big that you can’t contain them in any ordinary data types like a long integer. Use the power of Java’s BigInteger class and solve this problem.
Input Format
There will be two lines containing two numbers, a and b.
Constraints
a and b are non-negative integers and can have maximum 200 digits.
Output Format
Output two lines. The first line should contain a+b, and the second line should contain a*b. Don’t print any leading zeros.
Sample Input
1234
20
Sample Output
1254
24680
Explanation
1234+20 = 1254
1234*20 = 24680
Java BigInteger HackerRank Solutions
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner userInput = new Scanner(System.in); BigInteger a; BigInteger b; BigInteger sum = BigInteger.valueOf(0); BigInteger product = BigInteger.valueOf(0); a = new BigInteger(userInput.nextLine()); b = new BigInteger(userInput.nextLine()); sum = sum.add(a); sum = sum.add(b); product = a.multiply(b); System.out.println(sum); System.out.println(product); } }
Disclaimer: The above Problem (Java BigInteger) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.