c++ program to check whether a number is palindrome or not
using namespace std;
int n, num, digit, rev = 0;
cout << "Enter a positive number: ";
cin >> num;
n = num;
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome";
cout << " The number is not a palindrome";
Enter a positive number: 121
The reverse of the number is: 121
The number is a palindrome
- In this program user is asked to enter a positive number which is stored in the variable num.
- The number is then saved into another variable n to check it when the original number has been reversed.
- Inside the do...while loop, last digit of the number is separated using the code digit = num % 10;. This digit is then added to the rev variable.