Integer place values

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 3
Reputation: piggysmile is an unknown quantity at this point 
Solved Threads: 0
piggysmile piggysmile is offline Offline
Newbie Poster

Integer place values

 
0
  #1
Oct 15th, 2008
Is there an algorithm in C++ to determine the digit located on a given place value of an integer? For example, 9 would be outputted as the tens value of 196.

If none exists, is it possible to code it? If so, what are some of your ideas to do it?
Last edited by piggysmile; Oct 15th, 2008 at 3:52 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Integer place values

 
0
  #2
Oct 15th, 2008
If you want to determine n-th digit (from the back, so second digit in 1234 is 3) in a number, you do this:
a = number%(10^n) (now your digit is in first place)
digit = a/(10^(n-1)) (now everything is stripped except your digit)

That's the algorithm
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: Integer place values

 
0
  #3
Oct 15th, 2008
You can also convert the integer into a string, and then retrieve the particular nth placed digit of the string.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC