Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. // you implement this
  2. //To store result of addition
  3. string add_result="";
  4. //stores the sum of digits
  5. int sum_of_digits=0,len_of_b1,len_of_b2;
  6. len_of_b1=b1.size()-1;
  7. len_of_b2=b2.size()-1;
  8. while(len_of_b1>=0 || len_of_b2>=0 || sum_of_digits==1){
  9. //find sum of two digits
  10. if(len_of_b1>=0){
  11. sum_of_digits+=b1[len_of_b1]-'0';
  12. }
  13. else{
  14. sum_of_digits+=0;
  15. }
  16.  
  17. if(len_of_b2>=0){
  18. sum_of_digits+=b2[len_of_b2]-'0';
  19. }
  20. else{
  21. sum_of_digits+=0;
  22. }
  23.  
  24. //calculated sum is 3 or 1
  25. //then ,concatenate the result as
  26. add_result=char(sum_of_digits%2+'0')+add_result;
  27.  
  28. //then find the carry
  29. sum_of_digits=sum_of_digits/2;
  30. //then decrement len_of_b1 and len_of_b2
  31. //to go to next digits
  32. len_of_b1--;
  33. len_of_b2--;
  34.  
  35. }
  36. return add_result;
  37.  
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement