-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd.cpp
More file actions
35 lines (35 loc) · 897 Bytes
/
add.cpp
File metadata and controls
35 lines (35 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
string a,b;
cin>>a>>b;
int c=0,ka=a.length()-1,kb=b.length()-1,s=0; // c is for carry initially it is 0
string ans=""; // variable ans to store the answer
while(ka>=0||kb>=0)
{
if(ka>=0&&kb>=0)
s=c+(a[ka--]-'0')+(b[kb--]-'0'); // variable s to store the sum of two digits
else
if(ka>=0)
s=c+(a[ka--]-'0');
else
if(kb>=0)
s=c+(b[kb--]-'0');
c=s/10;
s%=10;
char cc=s+48; // to convert integer to character we add 48
ans=cc+ans;
}
if(c>0) // if carry is positive then add it
{
char cc=c+48;
ans=cc+ans;
}
cout<<ans<<endl;
}
}