Wrong answer For The Next Palindrome

,

I have tried many inputs for my code and it works perfectly on my machine but gives wrong answer in Codechef judge.I have read the forum too and tried every corner case suggested by others.Please Help me.

My Code is:

#include<stdio.h>
#include<stdlib.h>


int num(unsigned long x);

int main()  
{  
  unsigned long t;  
  scanf("%lu",&t);  
  while(t--)  
  {  
   unsigned long k,m;  
   int sum=0,i,s,j,n;  
   char a[10];  
   scanf("%lu",&k);  
   jump:  
   k=k+1;  
   if(k>9)  
   {  
     while(sum==0)  
    {sum=1;  
     n=num(k);  
     sprintf(a,"%lu",k);  
     for(j=0;j<=n/2;j++)  
     {  
        if(a[j]==a[n-1-j])  
         {  
            s=1;  
         }  
        else  
         s=0;  
         sum=sum*s;  
           
         if(sum==0)  
         goto jump;  
     }  
     if(sum==1 && k>9)  
     printf("%lu\n",k);    
       
   }  
  }  
  else   
  goto jump;  
}  
return 0;  
     
}  
  

int num(unsigned long k)  
{  int i=0;  
   while(k!=0)  
  {  
    k=k/10;  
    i++;  
  }  
 return i;  
}

Please submit a valid link. Your link is not working.

You are using unsigned int for taking inputs. Unsigned ints can store numbers having atmost 10 digits (upto 4294967296). However, the numbers given in input data can have upto 1000000 digits. I would suggest storing the numbers as strings. If you are unfamiliar with using numbers as strings, have a look here : http://www.codechef.com/viewsolution/2177288

2 Likes