# Separating a number into its individual digits

Page 1 of 1

## 12 Replies - 26276 Views - Last Post: 12 October 2009 - 07:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=131642&amp;s=e95dafeda00c57b5165be743d9dc8fb0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 DangerD

Reputation: 0
• Posts: 26
• Joined: 01-September 09

# Separating a number into its individual digits

Posted 12 October 2009 - 04:29 PM

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.

Is This A Good Question/Topic? 0

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

### #2 sjr

Reputation: 2
• Posts: 19
• Joined: 09-October 09

## 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 brds

Reputation: 76
• Posts: 515
• Joined: 22-October 08

## 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 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## 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 DangerD

Reputation: 0
• Posts: 26
• Joined: 01-September 09

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

Reputation:

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

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 Nitsua185

Reputation: 1
• Posts: 44
• Joined: 24-February 09

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

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 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

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

Reputation:

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

### #10 DangerD

Reputation: 0
• Posts: 26
• Joined: 01-September 09

## 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++;
}
}

Reputation:

## 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 DangerD

Reputation: 0
• Posts: 26
• Joined: 01-September 09

## Re: Separating a number into its individual digits

Posted 12 October 2009 - 06:44 PM

I need to fix the loop condition ;p

### #13 sjr

Reputation: 2
• Posts: 19
• Joined: 09-October 09

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