0
class Solution {
     public String multiply(String num1, String num2) {
      
       long n1= multiply_utility(num1);
        long n2=multiply_utility(num2);
        long mul=n1 * n2;
        return Long.toString(mul);
        
    }
    public long multiply_utility(String n){
        long number=0;
        for(int i=0;i<n.length();i++){
            if(n.charAt(i)>='0' || n.charAt(i)>='9'){
                number=number*10+ (n.charAt(i)-'0');
            }
            System.out.println(number);
        }
        return number;
    } 
}

num1 =

"498828660196"

num2 =

"840477629533"

Output

"-3269442614257959980"

Expected

"419254329864656431168468"

Hope
  • 1
  • 2
  • 2
    That product won't fit into a long. Use [`BigInteger.multiply()`](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#multiply-java.math.BigInteger-) instead. – Robby Cornelissen Mar 20 '23 at 03:16
  • Did you check this. https://leetcode.com/problems/multiply-strings/solutions/17605/easiest-java-solution-with-graph-explanation/ – Ankit Wasankar Mar 20 '23 at 03:17
  • Depending on whether this is homework or something else, the intent may have been for you to implement a multiplication algorithm yourself. Carefully read the instructions so you don't end up handing in a solution that would be disqualified.. – harold Mar 20 '23 at 03:18
  • Not what you asked, but I feel sure that `if(n.charAt(i)>='0' || n.charAt(i)>='9')` was supposed to say `if(n.charAt(i)>='0' && n.charAt(i)<='9')` – Dawood ibn Kareem Mar 20 '23 at 04:39

0 Answers0