943,630 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 746
  • C++ RSS
Oct 15th, 2008
0

Integer place values

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
piggysmile is offline Offline
3 posts
since Oct 2008
Oct 15th, 2008
0

Re: Integer place values

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
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 15th, 2008
0

Re: Integer place values

You can also convert the integer into a string, and then retrieve the particular nth placed digit of the string.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Looking for assistance.
Next Thread in C++ Forum Timeline: undeclared identifier ostream





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC