For example, if I have 456, How can I split this and then let each column value be a separate number? The only way I can think of doing this would be to subtract '100' n times until that column is '0' and storing the number of subtractions, leaving '4' then repeating for the other columns. Is there a quicker way?

  • 1
    $\begingroup$ Why not divide by $100?$ $\endgroup$
    – saulspatz
    Nov 4 '18 at 14:36
  • 3
    $\begingroup$ What you describe is pretty much the normal straightforward way one would go about this, only its description (and implementation if you are going to program it) can be abbreviated using the notions of division with and without remainder and modulo, as described in the anwers. If the number happens to be in base 2 and you are doing it on a computer then there are also even faster shift operations. $\endgroup$
    – Nobody
    Nov 4 '18 at 20:27

You can use the operation "modulo". It calculates the remainder after you have divided by a number.

  1. So 456 modulo 10 is 6, now you have the first digit.
  2. Then you can divide 456 with 10 without remainder, you get 45.
  3. Now 45 modulo 10 gives 5, now you have second digit.
  4. Then you can divide 45 with 10 without remainder, you get 4.
  5. Now 4 modulo 10 gives 4, now you have last digit.
  • 7
    $\begingroup$ Note that the 'ten' comes from decimal. This works in other number systems and you'll use the base-number of that number system instead of 'ten'. $\endgroup$ Nov 5 '18 at 1:28
  • 1
    $\begingroup$ "Divide without remainder" is also known as "divide and floor". $\endgroup$
    – Nayuki
    Nov 5 '18 at 6:53
  • 2
    $\begingroup$ and 'divide by the base' is also known as 'shift right'. $\endgroup$
    – amI
    Nov 5 '18 at 7:30

Divide $456$ with $100$ without remainder, you get $4$ - the first digit

Now $456 - 4\cdot100 = 56$ - subtract $100$ times the first digit

Now divide $56$ with $10$ without remainder to get 5 - the second digit

Now do $56 - 5\cdot10 = 6$ - last digit

You can use $\mod{}$ to get them another way, from last (or first if you call it that way) to first digit

  • 1
    $\begingroup$ The "last (or first if you call it that way)" ambiguity is resolved by using "most significant" and "least significant". $\endgroup$
    – Ben Voigt
    Nov 5 '18 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.