Find the next smallest palindrome solution [Easy]

Problem

For a given positive integer K of not more than 5 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros.

CodeChef Problem Link

Input

The first line contains integer T, the number of test cases. Integers K are given in the next t lines.

Output

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

Sample Input
1
808
Sample Output
818
Solution

Code Implementation

#include <iostream>
using namespace std;

int reverseNum (int num) {
    int revnum=0, dgt;
    
    while(num>0) {
        dgt = num%10;
        num = num/10;
        revnum = revnum*10+dgt;
    }
    
    return revnum;
}

int main() {
    int T, K;
    cin>>T;
    
    while (T--) {
        cin>>K;
        K++;
        
        while (1) {
            if (K == reverseNum(K)) {
               cout<<K<<endl;
               break;
            }
            K++;
        }
    }
    
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *