+ 2
Whats wrong with this code?
#include <iostream> using namespace std; bool isPalindrome(int x) { int a=0 , b ; //complete the function while (x !=0){ b = x%10 ; a = a*10+b ; x/=10 ; } if (x==a){ return (true); } else {return (false);} } int main() { int n; cin >>n; if(isPalindrome(n)==true) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }
1 Réponse
+ 5
so the problem is that you are changing x value in the while loop in your function. At the return statement you don't have the original value to compare with... so you can create one more variable to store the original and use it to compare with a.