Program to find out Next Palindrome

Hi all programmers here!

Problem Statement :
A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.
Input

The first line contains integer t, the number of test cases. Followed by t lines containing integers K.
Output

For each K, output the smallest palindrome larger than K.
Example

Input:
2
808
2133

Output:
818
2222
Warning: large Input/Output data, be careful with certain languages

Source: PALIN Problem | CodeChef

For above Problem, I created following program and also even submitted at the site mentioned in Source but their machine is again and again giving some Runtime error. As per those folks, it is because I am doing some wrong in I/O handling but as per me , it is working fine in my machine. I am not able to trace it out. 😔 Anyone here any ideas what error could be?

My Program:
package code.chef;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author sookie
 */
public class NextPalindrome {
   public static void main(String[] args) throws IOException {
        
        int t, k;
        StringBuilder z;
        BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
        t = Integer.parseInt(bin.readLine());
        
        String inputs[] = new String[t];
        for(int i = 0; i < t; i++) {
            inputs[i] = bin.readLine();           
        }
        for (int i = 0; i < t; i++) {
          
            k=Integer.parseInt(inputs[i]);
           do{
              z=new StringBuilder();   
              k=k+1;
             
              z.append(k);
              z=z.reverse();
            
              if(Integer.parseInt(z.toString())==k){
               System.out.println(k);
               break;
             }else continue;
            }while(k>=1);
            
        }
    }
    
}
 
Output:
2
808
2222
818
2332
Thanks in advance for finding out at what case, it can give runtime error !

Replies

You are reading an archived discussion.

Related Posts

Hi , Please make me understand the output of the following code:- #include void main() { char *s1; char far *s2; char huge *s3; printf("%d %d %d ",sizeof(s1),sizeof(s2),sizeof(s3)); }
hey... i m thnkng abt topic 3d-technology 4 seminar gv me ur view abt dis topic.... it's gud...
hi how i can prevent someone in a local network from copy files like docs or video ????? i wanna make files for read but can not copy i am...
Please help me with this project....... The aim is to build a Green circuit that can optimize the energy utilization in a room by avoiding unnecessary energy usage. For the...
Hello everyone Iam doing my M.S in aerospace engineering at Chennai, India. I require a high-speed operating valve that can open and close more than 200 times a second. Kindly...