+ 1

Palindrome no.

Could someone help me? Nothing makes sense. 3/6. bool isPalindrome(int x) { //complete the function int n = x; int digit, reverse=0; do{ digit = x % 10; reverse = (reverse * 10) + digit; x = x / 10; }while(x!=0); //else{return false;} //cout<<n<<endl; //cout<<x<<endl; //cout<<reverse<<endl; //cout<<digit<<endl; }

9th Jan 2022, 4:41 PM
Amber Singh Rathour Pandey
Amber Singh Rathour Pandey - avatar
8 Answers
+ 1
Almost! The return statement is comparing the wrong variable with reverse.
19th Jan 2022, 6:06 PM
Brian
Brian - avatar
+ 2
"bool isPalindrome()" should return a boolean, it is not returning anything, is it? you will get the same result (3/6) with this function: bool isPalindrome () {return 0;} šŸ˜…šŸ˜…
9th Jan 2022, 4:59 PM
CGM
CGM - avatar
+ 2
Gentlemen, I got it. Thank you, Brian ! EUREKA! Eureka! šŸ˜‚
20th Jan 2022, 12:25 PM
Amber Singh Rathour Pandey
Amber Singh Rathour Pandey - avatar
+ 1
Did you lose mental track of your overall program plan? After you reverse the number, you need to return a boolean comparison of equality with the original number. If this algorithm is too complex to mentally track, then write down the variables on paper and track their values so you can see what is happening.
9th Jan 2022, 9:46 PM
Brian
Brian - avatar
+ 1
I managed to reverse the number, now I donā€™t know what to do, how do I tell the program to compare the original one and the reverse one and say true or correct, something like this!
16th Jan 2022, 5:25 PM
Amber Singh Rathour Pandey
Amber Singh Rathour Pandey - avatar
0
Add a return statement that returns the comparison between the original number and the reversed number. To compare, use the boolean comparison operator, ==.
16th Jan 2022, 6:52 PM
Brian
Brian - avatar
0
#include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int n = x; int digit, reverse=0; do{ digit = n % 10; reverse = (reverse * 10) + digit; n = n / 10; }while(n!=0); return n == reverse; //else{return false;} //cout<<n<<endl; cout<<x<<endl; cout<<reverse<<endl; //cout<<digit<<endl; } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; }
19th Jan 2022, 5:58 PM
Amber Singh Rathour Pandey
Amber Singh Rathour Pandey - avatar