Email: rakesh@xamnation.com         Phone/Whatsapp: +91 9988708161
interview questions

Check if number is palindrome or not in C++

What are Palindrome numbers?

Palindrome numbers are those whose reverse are identical to the number itself.

For example: 121, 13431, 999 are examples of palindrome numbers. Zero is a palindrome number.

  • Palindrome numbers from 1-10  : 9
  • Palindrome numbers from 1-100: 18
  • Palindrome numbers from 1-1000 : 108
  • Palindrome numbers from 1-10000 : 198

How to find whether a number is palindrome or not

Follow these steps to find whether a number is palindrome or not

  1. You can iterate the number simultaneously from first position to last position. Keep matching until both positions merge
  2. Second option is to reverse the number, and compare number with its reverse

How to find whether a number is palindrome or not in C++

Here is a detailed program to find whether number is a palindrome or not

#include <iostream>
  using namespace std;

 
int
main () 
{
  
int n, num, l, r = 0;
  
cout << "Enter any number: ";
  
cin >> num;
  
n = num;
  
  do
    
    {
      
l = num % 10;
      
r = (r * 10) + l;
      
num = num / 10;
    
}
  
while (num != 0);
  
cout << "The reverse of given number is: " << r << endl;
  
if (n == r)
    
    {
      
cout << "It is palindrome." << endl;
    
}
  
  else
    
    {
      
cout << "It is not palindrome." << endl;
    
}
  
 
return 0;

}


Contributor

This code is contributed by Mr Omkar Tukaram Ghate. Mr Omkar is currently final year Computer Science student from KIT College of Engineering. 

End note

In this article, we have covered how to find palindrome in a number in C++. This question is very important from placement interview perspective. 

If you have any confusion regarding this article, please do write in discussion thread below. You can also mail us to info@xamnation.com.

Leave a Comment

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