C++ Program to Check Whether a Number or String is a Palindrome or not (5 Ways)

In this program, we learn how to check whether a number or string is a Palindrome or not using C++ programming language.

What is Palindrome?

It is a string or number which is when read from starting or end it will spell the same.
For example:- radar, it will spell same whether we read it from start or end. There are many more like dad, Malayalam etc.

We will write the C++ program to check for both number and string.

we will discuss Five ways to write code for it:-

  • Checking a number is palindrome or not
    1. Using loop
    2. Using recursion
  • Checking a string is palindrome or not
    1. Using loop
    2. Using recursion
    3. Using an inbuilt function.

Let’s discuss all the ways one by one.

C++ Programming Code to Check Whether a Number or String is a Palindrome or not

Checking a number is palindrome or not

Using Loop

We will use the while loop to make this program.

We will take individual digits from the given number and then adding it to the product of reversed number and 10.

Code:-

#include <iostream>  

using namespace std;  

int main(){  
    int num,rem,rev=0,temp;  

    cout<<"Enter the Number: ";    
    cin>>num;  

    temp=num;    
    while(temp>0){    
        rem=temp%10;    
        rev=(rev*10)+rem;    
        temp=temp/10;    
    }    
    if(num==rev)    
        cout << num << " is a Palindrome Number." << endl;    
    else    
        cout << num << " is not a Palindrome Number." << endl;   

    return 0;  
}  

Output:-

Enter the Number: 85458
85458 is a Palindrome number.

Using Recursion

In this program, we will use reverseNumber() recursive function to get the reverse of the given number and then compare it to the given number.

Code:-

#include <iostream>  
#include <math.h>

using namespace std;  

int reverseNumber(int num){
    int digits = (int) log10(num);
 
    // Base condition
    if(num == 0)
        return 0;
 
    return ((num%10 * pow(10, digits)) + reverseNumber(num/10));
}

int main(){  
    int num,rev,sum=0,temp;  

    cout<<"Enter the Number: ";    
    cin>>num;  

    rev = reverseNumber(num);  
   
    if(num==rev)    
        cout << num << " is a Palindrome Number." << endl;    
    else    
        cout << num << " is not a Palindrome Number." << endl;   

    return 0;  
}  

Output:-

Enter the Number: 569965
569965 is a Palindrome Number.

Checking a string is palindrome or not

Using loop

In this program, we have used the for loop.

Inside for loop, it checks the elements which are equidistant from the center. And at any point they differ, it raises the flag and breaks from the loop.

#include <iostream>  
#include <string.h>

using namespace std;  

int main(){  

    char str[20];
    int flag = 0;
    
    cout << "Enter a string: ";
    cin >> str;
    
    int len = strlen(str);
    
    for(int i=0;i < len ;i++){
        if(str[i] != str[len-i-1]){
            flag = 1;
            break;
   }
}
    
    if (flag) {
        cout << str << " is not a palindrome." << endl; 
    }    
    else {
        cout << str << " is a palindrome." << endl; 
    }

    return 0;  
}  

Output:-

Enter a string: radar
radar is a palindrome.

Using Recursion

In this program, I have used the isPalindrome() recursive function.

In which, it compares the first and the last element string( passed in the parameters and it stops when first element and last element don’t match or start and end become equal.

#include <iostream>  
#include <string.h>

using namespace std;  

bool isPalindrome(const string &str, int start, int end)
{
    if (start >= end)   
        return true;
    if (str[start] != str[end])
        return false;
    return isPalindrome(str, ++start, --end);   
}


int main(){  
    char str[20];
    int flag = 0;
    
    cout << "Enter a string: ";
    cin >> str;
    
    int len = strlen(str)-1;
   
    if(isPalindrome(str,0,len))    
        cout << str << " is a Palindrome." << endl;    
    else    
        cout << str << " is not a Palindrome." << endl;   

    return 0;  
}  

Output:-

Enter a string: malyaylam
malyaylam is a Palindrome.

Using the in-build function

In this program, I have used the reverse() in-build function, which will reverse the given string. And later we compare it with the given string.

Code:-

#include<iostream>
#include<string.h> 
#include<algorithm>

using namespace std; 

int main() 
{ 
    string str, rev;

    cout << "Enter a string : ";
    cin >> str;
    
    rev = str;

    // inbuild reverse function 
    reverse(rev.begin(), rev.end()); 

    if (str == rev) {
        cout << str << " is a palindrome." << endl; 
    }    
    else {
        cout << str << " is not a palindrome." << endl; 
    }
    return 0; 
} 

Output:-

Enter a string: radar
radar is a Palindrome.

You can learn about many other C++ Programs Here.

Best Books for learning C++ programming language with Data Structure and Algorithms.

The following two tabs change content below.

Amit Rawat

Founder and Developer at SpiderLabWeb
I love to work on new projects and also work on my ideas. My main field of interest is Web Development.

You may also like...