Home > Back-end >  how to find out only one digit in a int number?
how to find out only one digit in a int number?

Time:06-30

Im new so if this question was already Asked (i didnt find it scrolling through the list of results though) please send me the link.

I got a math quiz and im to lazy to go through all the possibilities so i thought i can find a program instead. I know a bit about programming but not much.

Is it possible (and in what programming language, and how) to read only one digit, e.g at the 3rd Position, in a integer? And how is an integer actually saved, in a kind of array?

Thanks!

CodePudding user response:

You can get rid of any lower valued digit (the ones and tens if you only want the hundreds) by dividing with rounding/truncation. 1234/100 is 12 in most languages if you are doing integer division.
You can get rid of any higher valued digits by using taking the modulus. 12 % 10 is 2 in many languages; just find out how the modulus is done in yours. I use "modulus" meaning "divide and keep the rest only", i.e. it is the opposite of "divide with rounding"; that which is lost by rounding is the final result of the modulus.

The alternative is however to actually NOT see the input as a number and treat it as text. That way it is often easier to ignore the last 2 characters and all leading characters.

  • Related