+ 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; }

25th Jul 2022, 2:45 PM
PieRat
PieRat - avatar
7 Antworten
+ 3
First copy x to num; num = x; instead of x = num; And in loop x = x/10; instead of num=num/10
25th Jul 2022, 2:53 PM
Jayakrishna 🇮🇳
+ 2
You probably meant to write num = x; instead of x = num;
25th Jul 2022, 2:47 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 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?
25th Jul 2022, 2:46 PM
PieRat
PieRat - avatar
+ 1
Oh, yes, you need to work exclusively with x in the loop. Don't mix x and num!
25th Jul 2022, 2:55 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
thank you guys, it's working now!
25th Jul 2022, 3:02 PM
PieRat
PieRat - avatar
0
Ani Jona 🕊 I tried that too, but then it doesn't give any output for some reason
25th Jul 2022, 2:48 PM
PieRat
PieRat - avatar