palindrome

logistic_guy

Senior Member
Joined
Apr 17, 2024
Messages
2,214
A \(\displaystyle \text{palindrome}\) is a number or a text phrase that reads the same backward as forward. For example, each of the following five-digit integers is a \(\displaystyle \text{palindrome}\): \(\displaystyle 12321, 55555, 45554 \ \text{and} \ 11611\). Write a program in the \(\displaystyle \text{C}\) language that reads in a five-digit integer and determines whether or not it’s a \(\displaystyle \text{palindrome}\). [Hint: Use the division and remainder operators to separate the number into its individual digits.]
 
A \(\displaystyle \text{palindrome}\) is a number or a text phrase that reads the same backward as forward. For example, each of the following five-digit integers is a \(\displaystyle \text{palindrome}\): \(\displaystyle 12321, 55555, 45554 \ \text{and} \ 11611\). Write a program in the \(\displaystyle \text{C}\) language that reads in a five-digit integer and determines whether or not it’s a \(\displaystyle \text{palindrome}\). [Hint: Use the division and remainder operators to separate the number into its individual digits.]
Please show us what you have tried and exactly where you are stuck.

Please follow the rules of posting in this forum, as enunciated at:


Please share your work/thoughts about this problem
 
To warm up the audience, we first tell the main idea of this program.

Enter a \(\displaystyle 5\)-digit number: \(\displaystyle \textcolor{indigo}{\bold{00800}}\)
\(\displaystyle \textcolor{blue}{\bold{wOW}}\), it's a palindrome.

Enter a \(\displaystyle 5\)-digit number: \(\displaystyle \textcolor{red}{\bold{12345}}\)
\(\displaystyle \textcolor{blue}{\bold{Oops}}\), it's not a palindrome.
 
My idea at this point is like this:

Suppose someone entered \(\displaystyle \textcolor{blue}{98765}\). I will create two arrays and save this number inside them.

\(\displaystyle A = \big\{9,8,7,6,5\big\}\)

\(\displaystyle B = \big\{9,8,7,6,5\big\}\)

Then, I will compare the first two digits in \(\displaystyle A\) with the last two digits in \(\displaystyle B\).

Since \(\displaystyle A[9] \neq B[5]\), that's enough to give me the non-palindrome!
 
It is difficult to enter each digit of \(\displaystyle 98765\) in the array. How to resolve this issue? We will use the hint given in the exercise.

Now Imagine you have this number \(\displaystyle 9876.5\)

\(\displaystyle 9876.5 = \frac{98765}{10}\) and the remainder of \(\displaystyle \frac{98765}{10}\) is \(\displaystyle 5\).

Then, we can enter the last digit of \(\displaystyle 98765\) in the array. By doing this to all digits, we can enter all of them in the array.
 
Top