# Palindrome check for number or string in C++

In this tutorial, we are going to work on palindrome check for a given number or string in C++. This is a very frequently asked question in different interviews and written exams that are based on the programming language. You must be able to answer such questions.

Well, there is a bit difference in coding for Palindrome check in case of number and string. But the logic remains the same.

## Palindrome check for number and string in C++

All the codes I have made and tested in Code-blocks v 16.01. They are working just fine and you can also use any other C++ IDE. It will work without any issue.

### Definition

Palindrome can be a word or a number which when reversed in their order remains same as the original like 12321 and malayalam. It remains same on reversing.

### Code for a palindrome number check in C++

```#include <iostream>
using namespace std;

int main(){
int number,temp;
int check=0;
cout<<"enter a number ";
cin>>number;
temp=number;
while(temp!=0){
check=check*10+temp%10;     //reversing the number
temp=temp/10;
}
if(check==number)               // condition for palindrome
cout<<"The number is palindrome.";
else
cout<<"The number is not palindrome.";
return 0;
}
```

You may check this: To check if a number is a palindrome or not in C++

Methodology

The program accepts a number from the user. Then it reverses it by the following code

```while(temp!=0){
check=check*10+temp%10;     //reversing the number
temp=temp/10;
}```

Then it checks whether the entered number and the reverse number are same or not i.e. the definition of palindrome.

```if(check==number)               // condition for palindrome
cout<<"The number is palindrome.";
else
cout<<"The number is not palindrome.";```

Output example

```enter a number 54
The number is not palindrome.```

### Code for palindrome string check in C++

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

using namespace std;
int main(){
char string;
int check=0,len;
cout<<"enter a string ";
cin>>string;
len=strlen(string);

for(int i=0;i<len;i++){
if(string[i]!=string[len-i-1])
check=1;
}          if(check==0)               // condition check for palindrome
cout<<"The string is palindrome.";
else
cout<<"The string is not palindrome.";
return 0;
}
```

Methodology

The program accepts a string value from the user. Then check for each character starting from first with its corresponding character from the last of the string.

```for(int i=0;i<len;i++){
if(string[i]!=string[len-i-1])
check=1;
}```

The result is stored as value of the variable “check”. Initially, the value of it is 0 and if the above code detects any mismatch in the string (by the above code), then the value of check becomes 1.
Then we check for the value of check variable and print the result on the screen.

```if(check==0)               // condition check for palindrome
cout<<"The string is palindrome.";
else
cout<<"The string is not palindrome.";```

Output example

```enter a string malayalam
The string is palindrome.```

I hope you have now understood the program and now be able to do solve this problem. Dropdown your queries in the comment box below.