The java BigInteger.add(BigInteger val) method is used for Sum of two large numbers using Strings in Java.
- Enter the use input from Scanner(System.in) object.
- Here sc.nextLine() method return the line with input.
- Now create the two BigInteger object and pass both two large numbers.
- Next use java.math.BigInteger.add(BigInteger val) calculate the Arithmetic Addition of two BigInteger.
- Print the Sum of numbers.
import java.math.BigInteger;
import java.util.Scanner;
public class AddNumberTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the first large number");
String number1 = sc.nextLine();
System.out.println("Enter the second large number");
String number2 = sc.nextLine();
BigInteger firstNumber = new BigInteger(number1);
BigInteger secondNumber = new BigInteger(number2);
BigInteger addNumber = firstNumber.add(secondNumber);
System.out.println("Result of addition is :- " + addNumber);
sc.close();
}
}
Output :-
Enter the first large number
123456789
Enter the second large number
923847567
Result of addition is :- 1047304356