# Palindrom numbers

A palindromic number is a number (such as 626) that remains the same when its digits are reversed. Write a function that returns true if a given number is a palindrome, and false, if it is not. Complete the given function, so that the code in main works and results in the expected output. Sample Input: 13431 Sample Output: 13431 is a palindrome

2/2/2022 2:19:28 PM

Bilal Khan8 Answers

New AnswerOne "x%10" does not a reverse of a number make. If you want to work with numbers - and it seems the task asks you to -, then you need to collect the digits that you split off using the modulo operator in another variable. The result needs to be compared to the original value, thus x needs to be retained. So, set int y = x; In the while loop, collect the digits in rev by setting rev initially to zero, and multiply by ten in each iteration to shift the digits one position to the left before adding another digit: rev *= 10; rev += x % 10; x /= 10; until x == 0. Then "return y == rev;"

Its my code #include <iostream> using namespace std; bool isPalindrome(int x) { int rev; if(x>10){ while(x>=0){ //complete the function rev=x%10; x=x/10; if(rev==x){ return true; }else if(x==0){ return false; } } }else{ return true; } } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }