0

# C++

bool isPalindrome(int x) { int tem = x; int rev = 0; while( x > 0){ rev = 10 * rev + x % 10; x /= 10; } if(rev == tem) return true; return false; } Why we can't use x directly, why we created int temp to store int x ??

16th Feb 2022, 6:23 AM
Sabnis Ashutosh
0
I had used it's showing error !!
16th Feb 2022, 6:58 AM
Sabnis Ashutosh
0
If we don't declare int temp =x in first and directly take x .... This is my q !!
16th Feb 2022, 7:06 AM
Sabnis Ashutosh
16th Feb 2022, 7:08 AM
Sabnis Ashutosh
0
while ( x > 0 ) Aka x will always be zero after the while loop And rev will alway be the reverse number you entered So if you enter 6076 After while loop rev = 6706 x = 0 In other words only way to get true would be to enter 0
16th Feb 2022, 7:30 AM
Raul Ramirez
0
To reverse the base10 number, after getting the remainder (modulo 10) you are dividing it by 10. That shrinks the number by chopping off the right most digit (the ones column). By the time you have finished the reverse, the original number is chopped down to 0 digits. To now compare the reversed number, you need a copy of the original, luckily you made a copy tem = x so you can compare tem to rev.
16th Feb 2022, 9:05 AM