Java BigInteger in Java : Hacker Rank Solution : Digit Wood

Java
,

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

Sample Output

Explanation

1234 + 20 = 1254

1234 * 20 = 24680

SOLUTION:

import java.util.Scanner;
import java.math.BigInteger;

public class Solution {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        BigInteger firstNumber = scanner.nextBigInteger();
        BigInteger secondNumber = scanner.nextBigInteger();

        BigInteger sum = firstNumber.add(secondNumber);
        BigInteger product = firstNumber.multiply(secondNumber);

        System.out.println(sum);
        System.out.println(product);

        scanner.close();
    }
}

FOLLOW FOR MORE QUESTIONS AND SOLUTIONS | DIGIT WOOD

Leave a Reply

Your email address will not be published. Required fields are marked *