title: How to add two very large numbers irrespective of their size in Java without using BigInteger data type?
body: <p>I need to add two very large numbers without using <code>BigInteger</code>. I am taking two string parameters but the below code only works with strings of equal length otherwise it throws <code>IndexOutOfBoundsException</code>. How can I fix that by adding big numbers irrespective of their length?</p>
  
  <pre><code>public static String add(String a, String b) {
      int carry = 0;
      String result = "";
  
      for (int i = a.length() - 1; i &gt;= 0; i--) {
        int digitA = a.charAt(i) - 48;
        int digitB = b.charAt(i) - 48;
  
        int resultingNumber = digitA + digitB + carry;
        if (resultingNumber &gt;= 10) {
          result = (resultingNumber % 10) + result;
          carry = 1;
        } else {
          result = resultingNumber + result;
          carry = 0;
        }
      }
      if (carry &gt; 0) {
        result = carry + result;
      }
      return result;
  }
  </code></pre>