In this post we will understand how to check whether the given number is a Palindrome number. First let’s understand which number is a Palindrome number:
Palindrome number: Any number which is same if we write it in reverse order is a Palindrome number. For example: 121, 2552, 76567 etc.
Logic:
- User enters a number ‘n’.
- Save that number ‘n’ to ‘old_num’.
- Find reverse of ‘n’ by taking the remainder ‘num’ and add it to (num*10). Example: if remainder of 151 is 1 we make new number with remainder 1 multiplying it by 10 i.e. 1*10+1=11.
- And dividing ‘n’ by 10 while (n>0).
- Finally by checking the ‘reverse’ with ‘old-num’, we can find whether the given number is palindrome or not.
Program to check Palindrome number C++.
PROGRAM:
#include<iostream> using namespace std; int main() { int n, reverse=0,num=0,old_num; cout<<"Enter number: "; cin>>n; old_num=n; while(n>0) { num=n%10; reverse=num+(reverse*10); n=n/10; } if(reverse==old_num) cout<<old_num<<" is a Palindrome Number"; else cout<<old_num<<" is NOT a Palindrome Number"; return 0; }