Hello everyone,

I have begun learning C++ (version 5.02) at my college and I am trying to do a few exercises to have a better understanding of it.

The one below, however, has proven to be a tough nut to crack (at least, for me). I'd be glad if someone can help me out:

*A palindrome is a number or a text phrase that reads the same backward and forward. For example, each of the following five-digit integers is a palindrome: 12321, 66666, 34543. Write a program that reads in a five-digit integer and determines whether or not it is palindrome.*

I have begun learning C++ (version 5.02) at my college and I am trying to do a few exercises to have a better understanding of it.

The one below, however, has proven to be a tough nut to crack (at least, for me). I'd be glad if someone can help me out:

The current version of C++ is 1999, so I'm guessing 5.02 is the version of the compiler, and my gut tells me it's Borland. If it's right, I urge you to get a newer compiler. Borland C++ 5.02 is now 11 years old.

You can solve the problem in one of three ways. One involves inputting strings, another involves converting an integer to a string with sprintf(), and the third involves the modulo operator.

After you have the digits in an array, all you need to do is compare array[i] to~~array[n-i]~~ array[n-i-1]. n is the number of digits in the original value (always 5). i is a number between 0 and n/2. If all comparisons are equal, the number is a palindrome.

You can solve the problem in one of three ways. One involves inputting strings, another involves converting an integer to a string with sprintf(), and the third involves the modulo operator.

After you have the digits in an array, all you need to do is compare array[i] to

Last edited on

Just a quick note: helios is correct, however where he writes "array[n-i]" it should really be "array[n - i - 1]" since when i = 0, you don't want to try and access array[5] which is out of bounds.

Yes, indeed we are using Borland C++, the 1999 version...

I also forgot to mention that I am supposed to do this without arrays or strings.

Here is what I could come up with:

#include <stdio.h>

int main (void)

{

int number;

int a,b,c,d,e;

int lefttotal;

int righttotal;

printf ("Please enter the number you want to check!\n");

scanf ("%d", &number);

a = number % 10;

b = (number % 100) / 10;

c = (number % 1000) / 100;

d = (number % 10000) / 1000;

e = (number % 100000) / 10000;

lefttotal=a+b+c;

righttotal=c+d+e;

if (lefttotal == righttotal)

printf ("The number is a palindrome!\n");

else {

printf ("The number is not a palindrome!\n");

}

return 0;

}

I also forgot to mention that I am supposed to do this without arrays or strings.

Here is what I could come up with:

#include <stdio.h>

int main (void)

{

int number;

int a,b,c,d,e;

int lefttotal;

int righttotal;

printf ("Please enter the number you want to check!\n");

scanf ("%d", &number);

a = number % 10;

b = (number % 100) / 10;

c = (number % 1000) / 100;

d = (number % 10000) / 1000;

e = (number % 100000) / 10000;

lefttotal=a+b+c;

righttotal=c+d+e;

if (lefttotal == righttotal)

printf ("The number is a palindrome!\n");

else {

printf ("The number is not a palindrome!\n");

}

return 0;

}

Last edited on

I checked a few numbers whether the program can correctly determine if they are palindromes or not.

It appears that while 11111 is a palindrome, 89798 is not. I am not sure why...

Can it have something to do with integer's range?

EDIT: It appears an int's range is between -32768 and 32767. According to the program, 32823 is not a palindrome, but 32723 is.

What should I use instead of int, if this is causing the problem?

It appears that while 11111 is a palindrome, 89798 is not. I am not sure why...

Can it have something to do with integer's range?

EDIT: It appears an int's range is between -32768 and 32767. According to the program, 32823 is not a palindrome, but 32723 is.

What should I use instead of int, if this is causing the problem?

Last edited on

Your algorithm is completely wrong.

If you just sum the digits, of course you'll get both false positives (not sure why you're getting false negatives, but it doesn't matter, since the algorithm is broken, anyway):

12321 will be (lefttotal == righttotal)

21321 will be (lefttotal == righttotal)

03321 will be (lefttotal == righttotal)

Instead, try COMPARING the digits. Compare a to e and b to d. You don't need c.

If you just sum the digits, of course you'll get both false positives (not sure why you're getting false negatives, but it doesn't matter, since the algorithm is broken, anyway):

12321 will be (lefttotal == righttotal)

21321 will be (lefttotal == righttotal)

03321 will be (lefttotal == righttotal)

Instead, try COMPARING the digits. Compare a to e and b to d. You don't need c.

Topic archived. No new replies allowed.