+ 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; }
7 odpowiedzi
+ 1
Almost!
The return statement is comparing the wrong variable with reverse.
+ 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;}
😅😅
+ 2
Gentlemen, I got it.
Thank you, Brian !
EUREKA! Eureka!
😂
+ 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.
+ 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!
0
Add a return statement that returns the comparison between the original number and the reversed number. To compare, use the boolean comparison operator, ==.
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;
}