+ 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; }
3 Respuestas
+ 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.
0
Thanku Shadow 😊
0
You are right Martin Taylor , thanks for this, from now onward, I'll always use string concept for palindrome number.