-->

Add large numbers

Posted by Admin on
Two strings representing two numbers have been provided as input. The numbers in the string can be so large that they may not be represented by the Java datatype int. The objective is to add the two numbers and output it as a string. You can assume that the numbers are non negative
add("123456776587789","23455876876896987")="23579333653484776"
add("10101","20202")="30303"




public class LargeAddition {
      
       static String testcase1 = "987";
       static String testcase2 = "987";
      
       public static void main(String args[]){
              LargeAddition testInstance = new LargeAddition();
              String result = testInstance.add(testcase1,testcase2);
              System.out.println("Result : "+result);
       }

       //write your code here
       public String add(String str1, String str2){
              int lenstr1=str1.length();
              int lenstr2=str2.length();
              int str1Iteration=lenstr1-1;
              int str2Iteration=lenstr2-1;
              int carry=0;
              int sum=0;
              String strNew="";
              while(str1Iteration>=0 && str2Iteration>=0)
              {
                     int digit1=str1.charAt(str1Iteration)-'0';
                     int digit2=str2.charAt(str2Iteration)-'0';
                     sum=digit1+digit2+carry;
                     int rem=sum%10;
                     carry=sum/10;
                     strNew=strNew+rem;
                     str1Iteration--;
                     str2Iteration--;
                 
              }
             
              //if str1 is greater than str2
              while(str1Iteration>=0)
              {
                     int digit1=str1.charAt(str1Iteration)-'0';
                     System.out.print("\ndigit1 "+digit1+" ");
                     sum=digit1+carry;
                     System.out.print("sum "+sum+" ");
                     int rem=sum%10;
                     carry=sum/10;
                     strNew=strNew+rem;
                     str1Iteration--;
                    
              }
             
              //if str2 is greater than str1
              while(str2Iteration>=0)
              {
                     int digit2=str2.charAt(str2Iteration)-'0';
                     sum=digit2+carry;
                     int rem=sum%10;
                     carry=sum/10;
                     strNew=strNew+rem;
                     str2Iteration--;
              }
             
              //if there is a carry at the end of addition 
              if(carry>0)
              {
                     strNew=strNew+carry;
              }
             
              strNew=reverse(strNew);
              return strNew;
       }
      
       public String reverse(String str){
              String strNew="";
              int len=str.length();
              for(int i=len-1;i>=0;i--)
              {
                     char ch=str.charAt(i);
                     strNew=strNew+ch;
              }
              return strNew;
       }

}

Please comment if you find any mistake.

No comments:

Post a Comment