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.
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.
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.