How to multiply two integers represented as strings and return the result also as a string in Java

1 Answer

0 votes
import java.math.BigInteger;

public class MultiplyStrings {
    public static String multiplyStrings(String num1, String num2) {
        // Convert strings to BigInteger
        BigInteger n1 = new BigInteger(num1);
        BigInteger n2 = new BigInteger(num2);

        // Perform multiplication
        BigInteger result = n1.multiply(n2);

        // Convert result back to string
        return result.toString();
    }

    public static void main(String[] args) {
        String num1 = "123";
        String num2 = "456";

        System.out.println("Result: " + multiplyStrings(num1, num2));
    }
}



/*
run:

Result: 56088

*/

 



answered Jun 5 by avibootz
...