I wrote a code for spoj.com
Adding reverse number
We have to input two numbers reverse both and add, and finally print the reverse of the sum.
I have scanned both numbers in form of string and used atoi and itoa functions several times.
This code works fine on Codeblocks 13.12 but shows compilation error on ideone or spoj(c++ 5.3.2).
prog.cpp: In function 'int main()':
prog.cpp:38:25: error: 'itoa' was not declared in this scope
itoa(sum,ssum,10);
Also am not able to find which g++
compiler Codeblocks is using.
I also tried adding stdlib.h
but it also don't work.
This is my code in c++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,num1,num2,sum,ln1,ln2,lns,i,j;
char snum1[100],snum2[100],ssum[100];
char stemp;
cin>>t; // scaning number of test cases
while(t--)
{
cin>>snum1>>snum2; // scanning number 1 and number 2 as string
ln1=strlen(snum1); //finding length of string of number 1
ln2=strlen(snum2); // finding length of string of number 1
for(i=0,j=ln1-1;i<ln1/2;i++,j--) // reversing the string of number 1
{
stemp=snum1[i];
snum1[i]=snum1[j];
snum1[j]=stemp;
}
for(i=0,j=ln2-1;i<ln2/2;i++,j--) // reversing string of number 2
{
stemp=snum2[i];
snum2[i]=snum2[j];
snum2[j]=stemp;
}
num1=atoi(snum1); //converting reversed string of number 1 to integer as num1
num2=atoi(snum2); // converting reversed string of number 2 to integer as num 2
sum=num1+num2; // finding sum
itoa(sum,ssum,10); // converting sum(integer) t0 string
lns=strlen(ssum); // finding length of string of sum
for(i=0,j=lns-1;i<lns/2;i++,j--) // reversing the sum string
{
stemp=ssum[i];
ssum[i]=ssum[j];
ssum[j]=stemp;
}
sum=atoi(ssum); // finally converting the reversed sum string to integer
cout<<sum<<endl; // final printing of result
}
return 0;
}