+ 2
Help with palindrome numbers?
Always outputs it's true #include <iostream> using namespace std; bool isPalindrome(int x) { int num, remain, rev = 0; x = num; do { remain = x % 10; rev = rev * 10 + remain; num = num / 10; } while (x != 0); if (rev == num){ 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 Réponses
+ 3
First copy x to num;
num = x; instead of x = num;
And in loop
x = x/10; instead of num=num/10
+ 2
You probably meant to write num = x; instead of x = num;
+ 1
I've been trying to solve this for hours now but it always says everything is a palindrome. Could anyone tell me what I'm doing wrong please?
+ 1
Oh, yes, you need to work exclusively with x in the loop. Don't mix x and num!
+ 1
thank you guys, it's working now!
0
Ani Jona 🕊 I tried that too, but then it doesn't give any output for some reason