How would you take an int and make each diget into a array element?

For example:

int num = 12345;
int nums[4];
// how ever you enter numbers into the array
// would result in:
//  nums[0] = 1
//  nums[1] = 2
// and so on...

Use mod % and division / operators. Since this is homework, sorry but I'm not going to say anything more about it until you post the code you have tried.

Here are some hints to get you started :

1) Given a number n, n mod 10, always results in the last digit of n. For example :

//mod = %
12345 % 10 equals 5
101011 % 10 equals 1
9234 % 10 equals 4
1 % 10 equals 1
0 % 10 equals 0

Now you have that fact. Here is one more:

Given a number n > 9, n/10 removes the last digit of n. If n is a 1 digit number,
then the result is 0. For example :

123/10 equals 12 //removed 3
100/10 equals 10 //removed 0
5234/10 equals 523 //removed 4
1/10 equals 0 //removed 1

Now you can use those facts to get each digit of a number. For example :

define n equal to 104
n mod 10 equals 4 and save 4
let n = n / 10 //n now equals 10 , 4 was removed
n mod 10 equals 0 and save 0
let n = n / 10 //n now equals 1, 0 was removed
n mod 10 equals 1 and save 1
let n = n / 10 // n now equals 0
since n is 0 STOP;

//now the digits we saved, do stuff with them

All of that is assuming integer arithmetic.


commented: Brilliant...really. -2

Learning programming is about solving problem not about asking for the exact answer.
firstPerson gave you a very big hint, now it is time for you to make effort to solve
this problem.

Good luck.

One more hint: you will need to know 4th grade math (add, subtract, multiply, and divide). What firstperson posted (except for the mod operator) was nothing more than what a 4th grader would learn in most USA schools.

Does mod mean the same as remainder?

Also, I have never taken a programming class before so please excuse my lapse's of knowlage.

int num=12345,aux,i;
int nums[5];



@ oieronle
We are here to help people with there code not give them the code if they don't have any. Secondly if you are going to post code please use code tags. Third your loop wont work like you think it does. When i is 1 it gets decremented to zero and then the for loop evaluates i and comes up false because i is 0 so you never get the last digit.

First of all, I'm sorry because of the revelation, and secondly, you are right, this code doesn't work propperly.

don't copy the code, but try to understand and try to solve it. Good luck


Does mod mean the same as remainder?

Also, I have never taken a programming class before so please excuse my lapse's of knowlage.

Yes mod returns the remainder from the division of its operands. For example,
10 mod 2 equals 0 because 2*5 equals 10, with 0 remainder. Another example,
5 mod 2 equals 1 because 2 goes into 5, 2 times with 1 remainder. Work it out on paper first and you will learn it better.

Ok, thanks alot. Now I got it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.