Answers for "palindrome number c++"

C++
1

next palindrome number in cpp

#include <iostream>


bool isPalindrome (int number) {
    int decomposed = number, reversed = 0;
    while (decomposed) {
        reversed = 10 * reversed + (decomposed % 10);
        decomposed /= 10;
    }
    return reversed == number;
}


int nextPalindromicNumber (int number) {
    while (!isPalindrome(++number));
    return number;
}


int main () {
    int testCases;
    std::cin >> testCases;
    while (testCases--) {
        int number;
        std::cin >> number;
        std::cout << nextPalindromicNumber(number) << '\n';
    }
}
Posted by: Guest on February-19-2021
0

c++ palindrome checker

#include <iostream>
using namespace std;

bool isPalindrome(int x) {
    int rem,reverse=0;
    while(x!=0){    
        rem=x%10;      
        reverse=reverse*10+rem;    
        x/=10;
        if (x == reverse) return true;
        else return false;
    }
    return true;
}

int main() {
    int n;
    cin >>n;
    
    if(isPalindrome(n)) {
        cout <<n<<" is a palindrome";
    }
    else {
        cout << n<<" is NOT a palindrome";
    }
    return 0;
}
Posted by: Guest on October-05-2021
0

palindrome c++

#include<iostream>
#include<string>
#include<algorithm>
bool IsPalindrome_true_false(const std::string& );
int main ()
{
    
    std::cout<<"Please enter a string:\t";
    std::string str;
    getline(std::cin, str);
    
    // convert the string from uppercase to lowercase 
    int i = 0;
    while(str[i])
    {
        if(str[i] == std::toupper(str[i]) && std::isalpha(str[i]) == 1024)
        str[i]+= 32;
        ++i;
    }
    // looping while string is empty 
    while(str.empty())
    {
        std::cout<<"\nPlease enter a string your string is empty:\t";
        if(!str.empty())
        std::string str;
        getline(std::cin, str);
    }
    
    std::cout<<"\n"<<std::boolalpha<<IsPalindrome_true_false(str)<<std::endl;
    std::cout<<std::endl;

    return 0;
}

// check if string is a palindrome and return true or false 
bool IsPalindrome_true_false(const std::string& str)
{

    int i = 0;                
    int j = str.length() - 1; 

    while(i <= j )
    {   
        
        if(std::isalpha(str[i]) == 0){
            ++i;
            continue;
        }else if(std::isalpha(str[j]) == 0){
            --j;
            continue;
        }   
        if(str[i] != str[j]){
        
           return false;
        }
        ++i;
        --j;
    }  
    return true;
}
Posted by: Guest on January-02-2021
0

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;}
     }
}
Posted by: Guest on February-19-2021
-1

palindrome cpp string

string input;

cout << "Please enter a string: ";
cin >> input;

if (input == string(input.rbegin(), input.rend())) {
    cout << input << " is a palindrome";
}
Posted by: Guest on June-10-2021
0

palindrome number c++

#include <iostream>
using namespace std;

int main()
{
     int n, num, digit, rev = 0;

     cout << "Enter a positive number: ";
     cin >> num;

     n = num;

     do
     {
         digit = num % 10;
         rev = (rev * 10) + digit;
         num = num / 10;
     } while (num != 0);

     cout << " The reverse of the number is: " << rev << endl;

     if (n == rev)
         cout << " The number is a palindrome.";
     else
         cout << " The number is not a palindrome.";

    return 0;
}
Posted by: Guest on October-19-2021

Browse Popular Code Answers by Language