stack and queue palindrome c++ code example

Example 1: how to print nth palindrome number in c++

int n;
    int count =0;
    while(true){
        cin>> n;
        if (n == 0) return 0;
        
        if( n <= 10000){
        for( int i=1 ; i <= 2000000000; i++){

                if( palindrom(i) == true) {
                    count++;
                    //cout << "palindrom : " << i<< " count : " << count <<endl;
                
                
                    if( count == n ) {
                    
                        cout << i <<endl;
                   
                        count =0;
                        break;
                    }

Example 2: next palindrome number in cpp

#include <iostream>
#include <string.h>
using namespace std;
int nextpalin (int num){
    while (num++) {
        string str = to_string (num); /// int to string conversion
        int l = str.length()-1;
        int s = 0;
        while( s<l ){
              if (str[s]!=str[l]) break;
              else {
                    s++;
                    l--;
                    }
        if (s>=l) return num;}
                }
   }

int main () {
    int t;
    cin >> t;
    while (t--){
    int num;
    cin >> num;
    if (num==0)  cout << "1" << endl;
   else { 
       if (num<9)  cout << num+1 << endl;
       else  cout << nextpalin( num) << endl;}
     }
}

Tags:

Cpp Example