Not taking into consideration specificities of languages (given the theme of Algoritmos
):
Your solution seems adequate. You can take user input as a string
and work with each character. Have, contact, care to make the calculations with the digits equivalent to those characters.
For example: if the user type 12345
, we would have A = 1
, nay A = '1'
(A
is the digit 1
, not the character '1'
which has decimal integer value equal to 49
, see to ASCII table).
Another possible solution would be to read the user input, character by character, in a loop. This solution is quite similar to the previous one. The biggest difference would be the fact that the input is taken character by character, not the full string.
A third output would be to take the integer value typed by the user and use entire divisions to find the characters of each position.
Thus, for an entrance 12345
and whereas:
- the operator
//
give us the result of the entire division;
- the operator
%
give us the result of the rest of the entire division.
We would have:
Entrada <- 12345
X <- Entrada
A <- X // 10000
X <- X % 10000
B <- X // 1000
X <- X % 1000
C <- X // 100
X <- X % 100
D <- X // 10
X <- X % 10
E <- X
There are probably other ways to perform this operation, but these are some that I’ve been able to think about.
I believe that the simplest (combining the issue of implementation and clarity of code for others) is the first, also suggested by you.
It can be in any language?
– rray
Yes @rray, no problem!
– Júlio César