This is my code for the problem.

[admin: hiding the code]

It works properly but it looks hideous. I am looking for some nice inputs to re-write it in a more elegant way.

1 Answer 1


Edit to rephrase, I hope this one is ok with the admin.

Operate on a copy of the number (so that you can use the number later for other things). Use a loop, can be a pretty simple while loop, and process only the last digit (% 10), or the last two digits within the loop. After that, divide the (copy of the) number by 10 if you processed one digit, or 100 if you processed two. Processing two at once has the advantage that you don't have to track whether a digit requires doubling or not.

You can do a similar loop to get the first two digits.

Those can be performed before the whole switch statement. I'd do some if-else chain instead, but why not.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .