Saturday 19 March 2016

The Next Palindrome


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, write the value of the smallest palindrome larger than K to output. 

Input 
The first line contains an integer, which corresponds to K. Assume that K is less than 200000. 

Output
Output consists of a single integer, which corresponds to the smallest palindrome larger than K.

Sample Input 1:
808

Sample Output 1:
818

Sample Input 2:
2133

Sample Output 2:
2222

Code:
  #include<stdio.h>
int main()
{
  long int num,pa=0,rem,temp;
  scanf("%ld",&num);
  while(num!=pa){
    num=num+1;
    temp=num;
    pa=0;
    while(temp!=0){
      rem=temp%10;
      pa=rem+pa*10;
      temp=temp/10;
    }
  }
  printf("%ld",pa);
  return 0;
}

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This will not work if a single digit is the input.

    ReplyDelete
    Replies
    1. add following code:
      if(a>0 && a<=9){
      printf("11");
      }

      Delete
  3. you check reverse and num is same after that once again asign reverse=0;It works correctly

    ReplyDelete