A palindrome is a number or a text phrase that reads the same backwards as forwards.

For example, each of the following five-digit integers is a palindrome:

12321, 55555, 45554 and 11611. Write a program that reads in a five-digit integer

and determines whether it is a palindrome. [Hint: Use the division and modulus

operators to separate the number into its individual digits.]

I'm having difficulty writing an algorithm to solve this problem (mostly separating the numbers) I know it has something to do with the decimal places but I'm having trouble.

I don't need somebody to write the program for me hopefully just a few more hints... thank you.

# Separating a number into its individual digits

## [Hint: Use the division and modulus operators to separate the number

Page 1 of 1## 12 Replies - 25573 Views - Last Post: 12 October 2009 - 07:46 PM

##
**Replies To:** Separating a number into its individual digits

### #2

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 04:37 PM

Are you not allowed to convert to string, reverse, convert back and compare for equality?

### #3

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 04:38 PM

It has nothing to do with the decimal places.

int a = 12345; int b = a % 10; /* b = 5 */ a /= 10; /* a = 1234, decimal places are dropped on conversion to int */ /*Wash, rinse, repeat */

### #4

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 04:40 PM

Each digits is a place right, so there is the 10's place 100's place 1000's place ect. think about it... Post some code if you need further help

### #5

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 04:40 PM

Thank you brds... I'll take a minute to soak in the code before I finish this exercise. If I have trouble I'll post my code...

This post has been edited by **DangerD**: 12 October 2009 - 04:42 PM

### #6 Guest_Neumann*

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 04:48 PM

Build up a second number in the reverse. Check to see if that number equals to the original one. If it does, then you got a palindrome.

Writing the most idiotic and thoughtless solution to this problem would defeat the whole purpose.

sjr, on 12 Oct, 2009 - 03:37 PM, said:

Are you not allowed to convert to string, reverse, convert back and compare for equality?

Writing the most idiotic and thoughtless solution to this problem would defeat the whole purpose.

### #7

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 05:16 PM

Neumann, on 12 Oct, 2009 - 03:48 PM, said:

Build up a second number in the reverse. Check to see if that number equals to the original one. If it does, then you got a palindrome.

Writing the most idiotic and thoughtless solution to this problem would defeat the whole purpose.

sjr, on 12 Oct, 2009 - 03:37 PM, said:

Are you not allowed to convert to string, reverse, convert back and compare for equality?

Writing the most idiotic and thoughtless solution to this problem would defeat the whole purpose.

Out of sheer curiousity, what exactly is wrong with that solution? That's the way i was thinking of doing it, you make me feel dumb

### #8

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 06:11 PM

If he said it nicely it would have been along the lines of:

Quote

While that certainly is one way to go about it, there are shorter and more efficient methods of solving this problem. Don't forget to take the hint into account.

### #9 Guest_Neumann*

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 06:16 PM

Not only such solution takes no thought, it relies on something that not every environment can provide. Namely a way to represent numbers as strings, reverse and compare strings.

This problem is a great way to practice working with numbers. All that's needed is 5-6 lines of simple arithmetic. Turning this into strings basically says "I have no idea how numbers are represented and how to extract their digits. I'll just let the language do all the work for me."

This is just my firm opinion, so don't let it make you feel stupid.

This problem is a great way to practice working with numbers. All that's needed is 5-6 lines of simple arithmetic. Turning this into strings basically says "I have no idea how numbers are represented and how to extract their digits. I'll just let the language do all the work for me."

This is just my firm opinion, so don't let it make you feel stupid.

### #10

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 06:23 PM

okay thanks for the help everyone... especially with the math parts (need alot of work in math ;p) I have completed a program that successfully determines if a number is or isn't a palindrome. I'm going to post the code I came up with now and would appreciate criticism on it and suggestions on how to improve it and what I should've done differently...

/* A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a five-digit integer and determines whether it is a palindrome. [Hint: Use the division and modulus operators to separate the number into its individual digits.] */ #include <iostream> void pali() { int digits[256]; int number; int i = 0; int j = 0; std::cout << "Enter a number: "; std::cin >> number; while (number / 10) { digits[i] = number % 10; number /= 10; i++; } while (i > 0) { if (j == 0) { if (number != digits[j]) { std::cout << "Number isn't a palindrome." << std::endl; return; } } else if (j != i) { if (digits[i] != digits[j]) { std::cout << "Number isn't a palindrome." << std::endl; return; } } else if (j == i && digits[i] == digits[j]) { std::cout << "Number is a palindrome!" << std::endl; return; } i--; j++; } }

### #11 Guest_Neumann*

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 06:32 PM

When I run your program it asks for a number. I enter it, but there's no output. For example, I enter 3. Instead of saying that 3 is a palindrome, it simply exits.

### #12

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 06:44 PM

I need to fix the loop condition ;p

### #13

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 07:46 PM

The more efficient approach doesn't require the array. I think the array makes it more like the string approach I mentioned, because

it treats the numbers as symbols ignoring their values. You can rebuild the reverse number using arithmetic too.

it treats the numbers as symbols ignoring their values. You can rebuild the reverse number using arithmetic too.

Page 1 of 1