Separating a number into its individual digits
[Hint: Use the division and modulus operators to separate the number
Page 1 of 112 Replies  18390 Views  Last Post: 12 October 2009  07:46 PM
#1
Separating a number into its individual digits
Posted 12 October 2009  04:29 PM
For example, each of the following fivedigit integers is a palindrome:
12321, 55555, 45554 and 11611. Write a program that reads in a fivedigit 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.
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
#3
Re: Separating a number into its individual digits
Posted 12 October 2009  04:38 PM
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
#5
Re: Separating a number into its individual digits
Posted 12 October 2009  04:40 PM
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
sjr, on 12 Oct, 2009  03:37 PM, said:
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:
sjr, on 12 Oct, 2009  03:37 PM, said:
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
Quote
#9 Guest_Neumann*
Re: Separating a number into its individual digits
Posted 12 October 2009  06:16 PM
This problem is a great way to practice working with numbers. All that's needed is 56 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
/* A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following fivedigit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads in a fivedigit 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
#12
Re: Separating a number into its individual digits
Posted 12 October 2009  06:44 PM
#13
Re: Separating a number into its individual digits
Posted 12 October 2009  07:46 PM
it treats the numbers as symbols ignoring their values. You can rebuild the reverse number using arithmetic too.
