0

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?

3
Contributors
2
Replies
12
Views
8 Years
Discussion Span
Last Post by Denniz
0

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

0

You can also convert the integer into a string, and then retrieve the particular nth placed digit of the string.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.