user3732708
user3732708

Reputation: 663

multiplying huge numbers in JAVA

I am trying to do this by add scanner to get user inputs, but I couldn't. You help me to improve this code by adding Scanner to get user input. I tried it but it get some error. I'm beginner to JAVA language.

import java.math.*;

public class BigIntegerMul {

public static void main(String args[]) {

    BigInteger int1 = new BigInteger("131224324234234234234313");
    BigInteger int2 = new BigInteger("13345663456346435648234313");
    BigInteger result = int1.multiply(int2);
    System.out.println(result);

}
}

Upvotes: 2

Views: 172

Answers (1)

Razib
Razib

Reputation: 11163

To take BigInteger using Scanner you may use the following code snippet -

import java.math.*;
import java.util.Scanner;

public class BigIntegerMul {

public static void main(String args[]) {

    //BigInteger int1 = new BigInteger("131224324234234234234313");
    //BigInteger int2 = new BigInteger("13345663456346435648234313");

    BigInteger int1, int2, result; 
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter first bigInteger: ");
    int1 = sc.nextBigInteger();

    System.out.println("Enter second bigInteger: ");
    int2 = sc.nextBigInteger();

    result = int1.multiply(int2);

    System.out.println("Result: " +result);

 }
}

Upvotes: 2

Related Questions