Separating a number into its individual digits

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

Page 1 of 1

12 Replies - 14760 Views - Last Post: 12 October 2009 - 07:46 PM Rate Topic: -----

#1 DangerD  Icon User is offline

  • New D.I.C Head
  • member icon

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

Separating a number into its individual digits

Post icon  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  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • 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 */


Was This Post Helpful? 2
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 DangerD  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Guest_Neumann*


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.

View Postsjr, 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.
Was This Post Helpful? 0

#7 Nitsua185  Icon User is offline

  • New D.I.C Head

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

Re: Separating a number into its individual digits

Posted 12 October 2009 - 05:16 PM

View PostNeumann, 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.

View Postsjr, 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 :blink:
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • 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.

Was This Post Helpful? 0
  • +
  • -

#9 Guest_Neumann*


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.
Was This Post Helpful? 0

#10 DangerD  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • 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++;
	} 
} 	

Was This Post Helpful? 0
  • +
  • -

#11 Guest_Neumann*


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.
Was This Post Helpful? 0

#12 DangerD  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#13 sjr  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1