+ 1

palindrome end of project, c++

I personally prefer the "string based" based path to completing this project, however this is only after I failed to adapt the crazy maths solution i found off the internet, I'm hoping someone can point out where I've gone wrong in trying to solve the bool type problem. #include <iostream> using namespace std; bool isPalindrome(int x) { int num, digit, rev; x = num; do { digit = num % 10; rev = (rev * 10) + digit; num = num / 10; } while (num != 0); if (x == rev) { 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; }

5th Mar 2021, 7:20 PM
Millü
Millü - avatar
3 odpowiedzi
+ 1
Hi! for a better understanding of how your code works, please use the "codes" section { }. place it there, make it pubic, and paste the link here
5th Mar 2021, 7:28 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
The assignment x = num; should be the other way round, i.e. num = x; and you need to initialize "rev" to 0, C++ does not do so automatically for you. Also, you can shorten your return statement to return ( x == rev );
5th Mar 2021, 7:48 PM
Shadow
Shadow - avatar