Converting huge numbers to array

I'm having problems with a task, which requires from me to find the factorial of a number n, and find the last non-zero digit of that number.
You'll probably think this problem is a piece of cake, but n can be a number from 1 to 1 000 000, so it makes this problem a bit tricky, since factorial of one million has at least 50 digits, far beyond long long.
A friend of mine told me that i could (while multiplying the numbers from 1 to n) store the result in an array, but i don't know how.
Can you please help me solve this problem?
you can use itoa to convert integer to array.
Ok, now for some helpful advice.

Consider how long-hand multiplication works. Forget about zeroes for now.


87
x 56
--
522
435
-----
4872


Notice that the ones digit is exclusively determined by multiplying the ones digit of both numbers and taking the ones digit of the result.

Now, to handle zeroes. Zeroes on the right hand side of a number just multiply by 10, which only adds a trailing zero and does not affect any other digits.

So make a for() loop from 2 to 1000000. Call this number X. Keep the product modulo 10 (call this number P).

At each iteration, find the first non-zero digit of X and multiply P by X. Then find the first non-zero digit of P and replace the value of P with that number.

After the for() loop terminates, P is your answer.
Topic archived. No new replies allowed.