An array is not strictly required to solve this.
You already have the mechanism to extract the last digit from a number. You just need to extend this by using use division to "shift" the digits the required number of places.
For example, if you have the number 1234
, to get the second digit you can divide by 10
, to get 123
. Using 123 mod 10
will give you the digit 3
.
To get the third and fourth digits you would just need to divide by 100
and 1000
respectively.
Note the pattern here. To get the digit at position 2 you divide by 100
, which is 10
raised to the power of 2
, or (10^2
). 1000
is 10
to the 3
or (10^3
), and so on.
Or more succinctly, you can expose the nth digit by dividing by the nth power of 10
.
Another different solution would be to use sprintf
to convert the number into a string. This hasn't been covered yet at this point in the course, but if you have some prior experience (or are feeling brave) then you might want to try that approach. Use man sprintf
from the command line to see how this might work.