Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- string add(string s1,string s2)
- {
- int i,j,k,sum=0,carry=0,len1,len2;
- string ans="";
- if(s1.length()>s2.length())
- swap(s1,s2);
- reverse(s1.begin(),s1.end());
- reverse(s2.begin(),s2.end());
- len1=s1.length();
- len2=s2.length();
- for(i=0;i<len1;i++)
- {
- sum=(s1[i]-'0' + s2[i]-'0')+carry;
- ans.push_back(sum%10 + '0');
- carry=sum/10;
- }
- for(i=len1;i<len2;i++)
- {
- sum=(s2[i]-'0')+carry;
- ans.push_back(sum%10 + '0');
- carry=sum/10;
- }
- if(carry)
- {
- ans.push_back(carry + '0');
- }
- reverse(ans.begin(),ans.end());
- return ans;
- }
- int main()
- {
- string ans="",s;
- while(cin>>s && s!="0")
- {
- ans=add(ans,s);
- }
- cout<<ans<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement