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; }
7 odpowiedzi
+ 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 );
+ 1
Ipang thanks, man but still not working
+ 1
Ipang
Here's a link
https://code.sololearn.com/csniYjgvqXCk/?ref=app
0
You issue `return result;` after the while...loop, premature return.
0
Ipang still not working
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
0
Oh gawd 😂. Now I'm battling with .NET, I guess it never ends