+ 2
What am I missing here...? Answered! Thank you everyone. You are incredibly fast in answering my questions!!
Hey guys. TRYING TO CREATE A BOOL FUNCTION HERE BUT SOMEWHERE THERE IS A MISTAKE... CANNOT FIGURE IT OUT. WHO CAN GIVE ME A HINT PLEASE? https://code.sololearn.com/c8Wc2aM7fKcJ/?ref=app
4 Réponses
+ 6
1) You are not returning anything if you *if* statement at like 12 is false.
Possible Fix: add a simple "return false" there
2) you are messing up your original number *x* during reversing it which will always fail the condition (x == reversenumber)
Possible Fix: do the operations on a duplicate instead
Here is how your program will look after applying all the fixes 👇
https://code.sololearn.com/cmxyn2sexKeU/?ref=app
+ 2
Thank you. Yes, I also tried your version of the duplicate before... :)
+ 1
#include <iostream>
using namespace std;
bool isPalindrome(int n)
{
int x = n;
int reversedNumber = 0, remainder;
while(x != 0)
{
remainder = x%10;
reversedNumber = reversedNumber*10 + remainder;
x /= 10;
}
return n == reversedNumber; //check if inputted number = reversedNumber
}
int main() {
int n = 111;
if(isPalindrome(n)) {
cout <<n<<" is a palindrome";
}
else {
cout << n<<" is NOT a palindrome";
}
return 0;
}