Why program is not working correctly | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why program is not working correctly

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

10th Jul 2021, 4:21 AM
Naman Chhabra
Naman Chhabra - avatar
3 Answers
+ 2
because you change x value to end at zero to get the reversed number, so when you compare x to reversed you compare 0 to reversed ^^ save your initial x in another variable, and compare it to reversed ;)
10th Jul 2021, 4:24 AM
visph
visph - avatar
+ 1
Done :) @visph
10th Jul 2021, 4:28 AM
Naman Chhabra
Naman Chhabra - avatar
0
you can also return reversed value and do comparison with return value and n ^^
10th Jul 2021, 4:25 AM
visph
visph - avatar