78.8k views
1 vote
Given two Strings String s1 = "11223351638791377189728193"; String s2 = "983763978669976862724569282405742578"; and String variable s3. Write a piece of code so that s3 refers to a String whose integer value will be the product of integer values of s1 and s2. Java.

User Anuj Gupta
by
4.6k points

1 Answer

1 vote

Answer:

import java.math.BigInteger;

public class Main {

public static void main(String[] args) {

String s1 = "11223351638791377189728193"; String s2 = "983763978669976862724569282405742578";

BigInteger num1 = new BigInteger(s1);

BigInteger num2 = new BigInteger(s2);

BigInteger product = num1.multiply(num2);

String s3 = product.toString(10);

System.out.println(s3);

}

}

Step-by-step explanation:

Because s1 and s2 contain large integers, the program need the biginteger module in other to compute the products of s1 and s2 when converted to integers.

This line initializes the two string variables s1 and s2

String s1 = "11223351638791377189728193"; String s2 = "983763978669976862724569282405742578";

The next two lines convert the two strings to BigInteger. This allows the program to multiply large integers

BigInteger a = new BigInteger(s1);

BigInteger b = new BigInteger(s2);

This computes the product of the two big integers

BigInteger product = num1.multiply(num2);

This converts the product to String s3

String s3 = product.toString(10);

This prints s3

System.out.println(s3);

}

}

User StefanHeimberg
by
4.3k points