+ 1

Palindrome number

In c++ project for palindrome number checking I have written this code but it's not passing some test cases like 8888 #include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int s = 0; while(x!=0){ s = s*10+x%10; x = x/10; } //cout<< s << " "; if(s==x){ return true ; } else return false ; } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }

18th Sep 2021, 1:12 PM
Anjali Singh
Anjali Singh - avatar
3 odpowiedzi
+ 4
After the while loop, `x` will be equal to zero, so the comparison to `s` will be always false unless it was zero initially. Make sure to preserve the original value of `x` for the comparison, for example using a second variable.
18th Sep 2021, 1:27 PM
Shadow
Shadow - avatar
0
Thanku Shadow 😊
19th Sep 2021, 7:26 AM
Anjali Singh
Anjali Singh - avatar
0
You are right Martin Taylor , thanks for this, from now onward, I'll always use string concept for palindrome number.
19th Sep 2021, 7:28 AM
Anjali Singh
Anjali Singh - avatar