Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // you implement this
- //To store result of addition
- string add_result="";
- //stores the sum of digits
- int sum_of_digits=0,len_of_b1,len_of_b2;
- len_of_b1=b1.size()-1;
- len_of_b2=b2.size()-1;
- while(len_of_b1>=0 || len_of_b2>=0 || sum_of_digits==1){
- //find sum of two digits
- if(len_of_b1>=0){
- sum_of_digits+=b1[len_of_b1]-'0';
- }
- else{
- sum_of_digits+=0;
- }
- if(len_of_b2>=0){
- sum_of_digits+=b2[len_of_b2]-'0';
- }
- else{
- sum_of_digits+=0;
- }
- //calculated sum is 3 or 1
- //then ,concatenate the result as
- add_result=char(sum_of_digits%2+'0')+add_result;
- //then find the carry
- sum_of_digits=sum_of_digits/2;
- //then decrement len_of_b1 and len_of_b2
- //to go to next digits
- len_of_b1--;
- len_of_b2--;
- }
- return add_result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement