0

What's wrong with this code? It's is supposed to state if a number if palindrome or not

include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int result = 0,remainder; while (x>0){ remainder = x%10; result = result * 10 + remainder; x/=10; } return result; if(result == 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; }

28th Mar 2022, 3:26 PM
Samuel Mbah
7 Réponses
+ 3
Oh I missed some issues #include <iostream> // missing the '#' Don't use <x> in the while loop, create and use a variable with copy of <x> value in place of <x> inside the while...loop block. The while...loop repeats while <x> not equal zero, so at the event loop finished, <x> will be zero, and your check for .`result == x` will fail. int result = 0, remainder, xcopy = x; while( xcopy > 0 ) { remainder = xcopy % 10; result = result * 10 + remainder; xcopy /=10; } return ( result == x );
28th Mar 2022, 3:41 PM
Ipang
+ 1
Ipang thanks, man but still not working
28th Mar 2022, 6:12 PM
Samuel Mbah
28th Mar 2022, 7:55 PM
Samuel Mbah
0
You issue `return result;` after the while...loop, premature return.
28th Mar 2022, 3:29 PM
Ipang
0
Ipang still not working
28th Mar 2022, 3:33 PM
Samuel Mbah
0
Save your code as a code bit, and share here a link to it. I can't verify the correctness of changes made when I can only see your original code in post's Description. https://www.sololearn.com/post/75089/?ref=app
28th Mar 2022, 7:22 PM
Ipang
0
Oh gawd 😂. Now I'm battling with .NET, I guess it never ends
1st Jan 2024, 11:56 PM
Samuel Mbah