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);

}

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);

}

Last edited on

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;

Last edited on

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?

for example 171 would just be 17 instead of 17.1. or do i have it wrong?

One more. You declared variable as having type double.

double x;

double palindrome = 0;

double check;

You shall declare them as integer and use operator % where I pointed out.

double x;

double palindrome = 0;

double check;

You shall declare them as integer and use operator % where I pointed out.

I declared

int x;

int palindrome = 0;

int check;

i have:

check = x % 10;

and now it won't recognize any integer as a palindrome.

int x;

int palindrome = 0;

int check;

i have:

check = x % 10;

and now it won't recognize any integer as a palindrome.

It looks like the algorithm is incorrect. Let consider your original code for x = 123. Initial value of polindrome is equal to 0.

Let go.

check = 12; // check = x / 10;

polindrome = 12; // palindrome = palindrome * 10 + check;

x = 12; // x = x / 10;

x == polindrome.

We have gotten invalid result because 123 is not a polidrome.

Let go.

check = 12; // check = x / 10;

polindrome = 12; // palindrome = palindrome * 10 + check;

x = 12; // x = x / 10;

x == polindrome.

We have gotten invalid result because 123 is not a polidrome.

that was why I was trying to use double instead of integer, so that

when x = 123:

check = 123/ 10, making check = 12.3

when x = 123:

check = 123/ 10, making check = 12.3

Topic archived. No new replies allowed.