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!
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
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?
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.
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.
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
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!