A Program to Check a Palindrome Number

In this example, we are giving a program to check whether a number is palindrome or not. A number can be said palindrome if the reverse of the number is equal to the actual number.


Program to check palindrome:

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4. int n, reverse_number = 0, remainder, actual_number;
  5. cout << "Enter an integer: ";
  6. cin >> n;
  7. actual_number = n;
  8. // reversed integer is stored in reverse_number
  9. while (n != 0) {
  10. remainder = n % 10;
  11. reverse_number = reverse_number * 10 + remainder;
  12. n /= 10;
  13. }
  14. // palindrome if actual_number and reverse_number are equal
  15. if (actual_number == reverse_number)
  16. cout << actual_number << " is a palindrome.";
  17. else
  18. cout << actual_number << " is not a palindrome.";
  19. return 0;
  20. }

Output:

Enter an integer: 3773
3773 is a palindrome.