Advertisement
Farjana_akter

Untitled

Feb 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. string add(string s1,string s2)
  4. {
  5. int i,j,k,sum=0,carry=0,len1,len2;
  6. string ans="";
  7. if(s1.length()>s2.length())
  8. swap(s1,s2);
  9. reverse(s1.begin(),s1.end());
  10. reverse(s2.begin(),s2.end());
  11. len1=s1.length();
  12. len2=s2.length();
  13. for(i=0;i<len1;i++)
  14. {
  15. sum=(s1[i]-'0' + s2[i]-'0')+carry;
  16. ans.push_back(sum%10 + '0');
  17. carry=sum/10;
  18. }
  19. for(i=len1;i<len2;i++)
  20. {
  21. sum=(s2[i]-'0')+carry;
  22. ans.push_back(sum%10 + '0');
  23. carry=sum/10;
  24. }
  25. if(carry)
  26. {
  27. ans.push_back(carry + '0');
  28. }
  29. reverse(ans.begin(),ans.end());
  30. return ans;
  31. }
  32. int main()
  33. {
  34. string ans="",s;
  35. while(cin>>s && s!="0")
  36. {
  37. ans=add(ans,s);
  38. }
  39. cout<<ans<<endl;
  40. return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement