My code is quite simple and straightforward. I get "wrong answer" on submission though. I have no clue why that happens! Here is the code...
#include<iostream>
#include<string>
using namespace std;
void sum(string num)
{
int i,len=num.length();
int j=len-1;
int carry=0;
string answer;
int s=0;
for(i=0,j;i<len;i++,j--)
{
s = (num[i]-'0')+(num[j]-'0')+carry;
if (s>10&&j!=0)
{
carry = s/10;
s = s%10;
}
else if (s>10&&j==0)
{
carry=s/10; s=s%10; answer+=s+'0';answer+=carry+'0'; break;
}
answer+=s+'0';
}
int sz=answer.size();for(int j=sz-1;j>=0;j--) cout<<answer[j];
}
int main(int argc,char **argv)
{
int n;cin>>n;
for(int i=0;i<n;i++)
{
string no;cin>>no;
sum(no);cout<<endl;
}
}