+ 3

Can Someone Please Explain the Palindrome Code Coach?

I was having trouble solving the palindrome code coach and ended up having to search for help on how to solve it. I was trying to use a for loop to reverse the numbers but then couldn't figure out how to return the variable back to main to determine if it was true or false. I was only getting half of the problem correct. I found this solution on the discussions and was trying to interpret it, but I don't understand what is happening after the first function is called. Can someone please break down each section as to what is happening so I can better understand this? Thank you in advance! #include <iostream> using namespace std; int revDigit(int y) { int rem, rev = 0; while(y>0) { rem = y % 10; rev = rev * 10 + rem; y /= 10; }return rev; } bool isPalindrome(int x) { if(x == revDigit(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; }

26th Jan 2022, 4:38 PM
Green Tea
Green Tea - avatar
4 Answers
+ 3
palindrome checks if two numbers are the same if they are reversed. isPalindrome function checks if x number is the same as revDigit, which outputs the reverse of the digit. how revDigit works is by taking the mod of 10 from y which is the rightmost number to rem. so if y = 456 rem = y % 10 // rem value will be 6 then rev would multiply by 10 to give space to the rightmost number and add rev so rev would first be rev = 6 rev = 60 + 5 = 65 rev = 650 + 4 = 654 keep looping and make y divide by 10 to remove the rightmost number until y turns 0. and you get rev which is the reverse number.
26th Jan 2022, 6:19 PM
Shen Bapiro
Shen Bapiro - avatar
+ 1
Is it failing any test cases? edit: @Trevor Biroschik * first taking input * it is calling isPolindrome(n) on input, which returns true or false about Palindrome correctness, by checking input x to value returned from * another function revDigits(x) which returns a reversed value of input , within the function ex: 234 to 432 ex2: 232 to 232 first example returns false and next returns true... edit: It would be better to tell, which specific lines of code you are not understanding... hope it clears..
26th Jan 2022, 4:42 PM
Jayakrishna 🇼🇳
+ 1
G'day Trevor Biroschik did you understand the rev=rev*10 +(y%10) part? To send the result of the function back, you use "return (variable_name)" in this case it is 👉return rev👈 . To ask for that return, the isPalindrome function has the line 👉if(x == revDigit(x))👈 so the return of revDigit will be inserted in to that line. It works out to be if(x == (the return value from the revDigit function) Does that explain "return" ok?
26th Jan 2022, 9:54 PM
HungryTradie
HungryTradie - avatar
+ 1
@Shen thank you so much! That makes sense to me now. @Jayakrishna your explanation was well said piece by piece and I thank you for that. Pretty much I wanted to understand when the revDigit(x) function was called. But I understand now that it's being called while also being compared to x. @Hungry that does explain the return, thank you so much!
27th Jan 2022, 3:05 PM
Green Tea
Green Tea - avatar