I have a function that i've got to write as part of a program. The function is designed to check and see if an integer is a palindrome (same forward as backwards, i.e. 1551 or 60906). My problem i'm running into is that if i enter an integer that is NOT a palindrome (i.e. 12345) it still tells me that the integer i've entered is a palindrome. The algorithm i'm using is about the same as any others i've found on various websites and either all of those are wrong too and no one realizes it or i'm doing something terribly wrong (probably the latter). Does anyone know what I am doing wrong?
Here is my source code for the function:
void reverse()
{
double x;
double palindrome = 0;
double check;
cout << "Enter the integer: ";
cin >> x;
do
{
check = x / 10;
palindrome = palindrome * 10 + check;
x = x / 10;
if ( x == palindrome)
{
cout << "This integer is a palindrome!\n";
break;
}
else if ( x != palindrome )
{
cout << "This integer is not a palindrome!\n";
break;
}
}while ( x > 0);
}
The code is invalid. First of all you shall use integer variables instead of floating point variables. Secondly instead of check = x / 10; you shall use check = x % 10;
i did that at first but it still did the same thing. And if i'm using it as an integer variable, when you divide by ten wouldn't it add a decimal into the integer?
for example 171 would just be 17 instead of 17.1. or do i have it wrong?