# {HELP} - What's wrong in this code for THE NEXT PALINDROME problem ?

``````//JUST LIKE ANIMALS !!!!

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;

bool ispalin(ll n){
ll i;
string s=to_string(n);
for(i=0;i<(s.length()/2);i++){
if(s[i]!=s[s.length()-1-i]){
return false;
}
}
return true;
``````

}

``````int main(){
int t;cin>>t;
while(t--){
ll n,i;cin>>n;
if(n<10)cout<<n+1<<endl;
else
if(n%99==0&&n%11==0)cout<<n+2<<endl;
else{
for(i=n+1;;i++){
if(ispalin(i)==true)
{
cout<<i<<endl;break;
}
}
}
}
return 0;
``````

}

The mistake which I can see is that, for the test case :-
1
5

It gives the output as 11, but the output should be 6, because single digit numbers are by default palindrome.

Corrected it , still WA

You should link the problem url? Since i have done similar problem in the past, i would assume it is http://www.spoj.com/problems/PALIN/
Note this line which says “positive integer K of not more than 1000000 digits”, you simply cannot take the input to an integer. Do it as a string problem

1 Like

Your second condition is wrong,for inputs like 9900 it will not give correct answer it gives 9902 but which is not correct. I think this makes you clear what is the mistake in your code

1 Like

Can anybody help me

NZEC is coming for The Next Palindrome

import java.util.*;
class Main
{

``````	public static boolean palin(long a){
long right=a;
long rev=0;
while(a>=10){
rev=rev*10+a%10;
a=a/10;

}
rev=rev*10+a;
if(rev==right)
return true;
else
return false;
}

public static void main (String[] args) throws java.lang.Exception
{
Scanner scan=new Scanner(System.in);
int turn=scan.nextInt();
for(int i=0;i<turn;i++){
long num=scan.nextLong();
while(palin(++num)!=true){
}
System.out.println(num);
}
``````

}
}