What is Palindrome?
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar. There are also numeric palindromes such as 1551,121 and so on. But here we will check whether the input number is palindrome or not.
Program:
#include <iostream>
using namespace std;
int main()
{
int n, num, rem, rev = 0;
cout << "Enter a positive number: ";
cin >> num;
n = num;
while (num != 0) //loop to find reverse of num
{
rem = num % 10;
rev = (rev * 10) + rem;
num = num / 10;
}
cout << " The reverse of the number is: " << rev << endl;
if (n == rev) //check if num is equal to reverse of num if it is equal then num is palindrome
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";
return 0;
}
Output:
Enter a positive number: 121
The reverse of the number is: 121
The number is a palindrome.
C++ program to check whether a number is Palindrome or not
Reviewed by Beast Coder
on
February 01, 2021
Rating:
No comments: