# Storing a single number as an array

• 09-30-2011, 04:27 PM
Inferno719
Storing a single number as an array
Hello. I need to store a single number as an array. I will use an array 100 elements long to store the individual digits of a number, and then run arithmetic against it. Addition, subtraction, multiplication, division, modulus, and factorial. The thing is, I can't think of a way to get the number into the array without asking the user to manually enter every digit, which is not acceptable in the context.

Any insight would be greatly appreciated!
• 09-30-2011, 04:36 PM
JosAH
Re: Storing a single number as an array
If using a BigInteger is not an option for you, have a look at the % operator x%10 takes the rightmost digit from the int x and x/10 removes the rightmost digit from the int x.

kind regards,

Jos
• 09-30-2011, 04:47 PM
Inferno719
Re: Storing a single number as an array
Oh, that makes a lot of sense actually. But how would i store the integer between moduluses?
• 09-30-2011, 04:54 PM
JosAH
Re: Storing a single number as an array
Quote:

Originally Posted by Inferno719
Oh, that makes a lot of sense actually. But how would i store the integer between moduluses?

I should leave this up to you, but just because I'm such a nice and friendly person, I give you this pseudo code:

Code:

```do {   store x%10 in the next array position   x= x/10 } while (x != 0);```
kind regards,

Jos

ps. I'm moving this thread to the 'New to Java' section because there's nothing advanced about it.
• 09-30-2011, 10:47 PM
Inferno719
Re: Storing a single number as an array
Thanks! I'll try to make this work.

My apologies, I'm taking what amounts to intermediate java and I wasn't sure where to put it.
• 10-01-2011, 07:22 AM
JosAH
Re: Storing a single number as an array
Quote:

Originally Posted by Inferno719
My apologies, I'm taking what amounts to intermediate java and I wasn't sure where to put it.

There's no need to apologize; it was my opinion that your question didn't belong in the 'advanced' section so I moved it. None of the other moderators jumped on me so I think they agree. If you have troubles with your code, feel free to post your question(s) here again.

kind regards,

Jos
• 10-03-2011, 04:58 AM
Inferno719
Re: Storing a single number as an array
Sorry, disregard this. Edited to inform you I have solved the problem on my own. (I passed it as a String instead, performed a few for loops and some somewhat complicated coding to strip out individual characters from a string, added it to a feeder array, and fed the feeder array to the int array through a toString.)

Thanks for the help everyone!