I guess my code is correct as it is giving correct answers for most cases. Can anyone point out any mistakes?
code:
#include<iostream>
using namespace std;
int digits(int a)
{
int d=0;
while(a!=0)
{
a=a/10;
d++;
}
return d;
}
int sepa(int a)
{
int d=digits(a);
if(d%2==0)
{
d=d/2;
}
else{
d=(d-1)/2;
}
while(d!=0)
{
a=a/10;
d--;
}
return a;
}
int rev(int a)
{
int r;
int t;
int d=digits(a);
while(d!=0)
{
r = r*10;
r += a%10;
a=a/10;
d--;
}
return r;
}
int pali(int a)
{
int d=digits(a);
d=d/2;
int t = sepa(a);
if(digits(a)%2!=0)
{
t=t/10;
t=t*10;
}
while(d!=0)
{
t=t*10;
d--;
}
d = t + rev(sepa(a));
return d;
}
int main()
{
int c,d,tc;
cin>>tc;
while(tc!=0)
{
cin>>c;
d=pali(c);
while(c>=d)
{
int t=sepa(d)+1;
int r=digits(d)/2;
while(r!=0)
{
t=t*10;
r--;
}
d=pali(t);
}
cout<<d<<endl;
tc--;
}
return 0;
}