Palindrome
A palindrome is anything that reads the same backward and forward. In other words after reversing any number, word or phrase if it remains the same as original then it is a palindrome.
Logic
To check whether a number is a palindrome number or not we have to reverse it first. After reversing it we compare the reversed number with the original number. If both the numbers are similar then it means the number we have is a palindrome number.
Program
#include <iostream> using namespace std; int main() { int num, temp, reverse = 0; // Take input from user cout << "Enter any number : "; cin >> num; temp = num; while(temp != 0) { reverse = (reverse * 10) + (temp % 10); temp /= 10; } // Check for palindrome if(reverse == num) { cout << endl << num << " is a palindrome."; } else { cout << endl << num << " is not a palindrome."; } return 0; }
Output
Enter any number : 121
121 is a palindrome.