# HELP Please- Putting Digits within a number in ascending order

• March 21st, 2014, 01:28 AM
Java girl
HELP Please- Putting Digits within a number in ascending order
for example, if i am given 9864
i must output 4689

Without use of arrays.
• March 21st, 2014, 01:41 AM
dicdic
Re: HELP Please- Putting Digits within a number in ascending order
there are many approach with that kind of problem.
The first approach that I thought is:
I have int var = 1; int answer = 0;
then I'll get the first digit, and multiply it by var.
and then I'll add it to answer; answer now is 9

next I'll multiply the var by 10, var now is 10
then I'll get the 2nd digit and multiply it by var, it will be 80 right?
then I'll add it to answer whose current value is 9, answer now is 89

next I'll multiply the var again by 10, var now is 100
then I'll get the 3rd digit and multiply it by var, it will be 600 right?
then I'll add it to answer whose current value is 89, answer now is 689

next I'll multiply the var again by 10, var now is 1000
then I'll get the 4th digit and multiply it by var, it will be 4000 right?
then I'll add it to answer whose current value is 689,answer now is 4689

get the logic?

the other approach is to get each character and store to another string.
I'll have String answer whose value is "",
then get the first digit.
make the answer first digit concatenated by the current value of answer String.
then get the next digit and make the answer equal to the next digit concatenated by the current value of answer String
repeat....
• March 21st, 2014, 02:46 AM
Java girl
Re: HELP Please- Putting Digits within a number in ascending order
Thanks