#include <iostream>
using namespace std;
bool pals(int n) {
int remainder;
int reverse=0;
while(n>0){
remainder=n%10;
reverse=reverse*10+remainder;
n=n/10;
}
return reverse;
}
int main() {
int nums;
cout<<"Input Desired Number: ";
cin >>nums;
if(pals(nums)) {
cout <<nums<<" palindrome";
}
else {
cout <<nums<<" not a palindrome";
}
return 0;
}
Keeps returning " palindrome" when it isn't.
Is there something missing in my code or something wrong.
Any tips or pointers for future references would be good too. Thank you!
Any feedback is great! Thank You!
Your problem is because you are returning a number from the function and casting it to a Boolean so every number will truncated to a True value.
You should check inside the function whether it’s a palindrome or not and return a 1/0 result or even better a Boolean value.
A thorough explanation on the behavior described can be found in the following link: