C++ palindrome number help! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C++ palindrome number help!

The test case 3 and 4 (which are hidden) are not working with this code. Can anyone please tell me what's wrong with the code? Thankyou. #include <iostream> #include <cmath> using namespace std; bool isPalindrome(int x) { //complete the function int len = sizeof(x); int revSum = 0; int tempX = x; for (int i = 0; i < len; i++) { revSum += (tempX % 10) * pow(10, len - i - 1); tempX /= 10; } if (revSum == 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; }

4th Jun 2021, 1:01 PM
Aarya Parekh
Aarya Parekh - avatar
2 Answers
+ 3
Aarya Parekh , the calculation for variable <resSum> is not correct. for input: 11 it shows a final value of 1100. so i recommend you to rework this. happy coding and good success!
4th Jun 2021, 2:40 PM
Lothar
Lothar - avatar
+ 2
you doesn't need len = sizeof(x), wich get the byte size of the variable (int) and you should use a 'while (tempX)' loop rather than a 'for' loop... also, you formula to compute revSum is wrong ^^ here's a corrected version of your code (keeping old changed code but commented): https://code.sololearn.com/c6IkQQtPhaGD/?ref=app
4th Jun 2021, 3:30 PM
visph
visph - avatar