# Itoa algorithm?

I was wondering how you could (in c/c++ or any language) take an integer like 12345 and convert it into the string "12345" like the C function "Itoa" does.  It would be great if someone could give me step by step instructions or point me to somewhere on the web.  I couldn't find the algorithm myself.
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

EngineerCommented:
The basic algorithm is to take the number and divide it by 10. That gives you two components: quotient and remainder. The remainder is the number between 0 and 9 that you can convert a character by adding it to '0'. Repeat until the quotient is 0.

The only tricky part is that as you keep doing this, the remainder is given in backward order.

For example, let's use 12345.

12345 / 10 = 1234
12345 % 10 = 5       (% is modulus operator, gives remainder)

1234 / 10 = 123
1234 % 10 = 4

123 / 10 = 12
123 % 10 = 3

12 / 10 = 1
12 % 10 = 2

1 / 10 = 0
1 % 10 = 1

0

Experts Exchange Solution brought to you by