# How to convert the digits of a number into elements of array?

For example in pset1 hacker edition program asks user to input the number of credit card. Imagine that a digits are: 1234567890 So can anyone tell me does it possible to convert this number into elements of array {1,2,3,4,5,6,7,8,9,0} and if it possible than how?

in my code it looks like this:

//askin the user to input number

``````long long cc_num; //here is another question do i need to declare cc_num equals zero?
do{
printf("number:);
cc_num = GetLongLong();
}
while (cc_num < 10 && cc_num > 10); //we assume that number always be 10 digits
``````

//so here i want to declare an array using the digits from cc_num like elements of an array

``````     int array;
for (int i = 0, n = strlen(cc_num); i < n; i++)
{
array[i] = ???

for (i = 9; i > 0; i -= 2) //here i want to start counting from second-to-last element
{
//multiply each second element by 2
//sum multiplyed elements = a
}
for (i = 10; i >= 0; i -= 2) //here i want to start counting from last element to get result of sum non-multiplyed elements
{
//sum non-multiplyed elements = b
}
//sum multiplyed and non-multiplyed  (a + b)

}
``````

i have already spent 2 days and 2 nights solving this problem...heeeelp me

• Logically speaking, this condition `(cc_num < 10 && cc_num > 10)` is always false because if `cc_num` is < 10, the left sub-condition is true and the other is false and if it's > 10, the right sub-condition is true and the other is false and if it's 10, the both are false.
– kzidane
Mar 17, 2015 at 9:22
• What if i use || instead &&? Mar 17, 2015 at 13:54
• That would work, but a better solution would be just `cc_num != 10`! ;)
– kzidane
Mar 17, 2015 at 14:07
• i understood, but what if i assume that the length of number might be 13, 14 or 16 digits. does this condition be correct (cc_num < 13 || cc_num > 16 || cc_num = 15)? Mar 17, 2015 at 14:50
• or maybe another one (cc_num != 13 || cc_num != 16 || cc_num =15)? Mar 17, 2015 at 14:52

If you want to store each digit of a number as an element of an array you can do the following.

``````long long number = 1234567890;
int digits;

for (int i = 0; i < 10; i++)
{
digits[i] = number % 10;
number = number / 10;
}
// At the end digits = {0, 9, 8, 7, 6, 5, 4, 3, 2, 1}
``````

But if you want the elemnts to keep the same order than the original number you can do this instead:

``````for (int i = 9; i >= 0; i--)
{
digits[i] = number % 10;
number = number / 10;
}

// At the end digits = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
``````

Why not input the number as a string to begin with? (This would also make it easier to check the length is right - you seem to be comparing the size of the number, rather than its length, with 10). Then it is already an array.

Of course then you will store the ascii codes of the digits initially, rather than their values, but the conversion is easy.

• Though if you want to do it your original way, sprintf() can format numbers into strings. Mar 17, 2015 at 9:10
• i forgot to use ' ' and i don`t really sure how to implement the convertion, do i need to declare each digit (0-9) like it`s represented in ascii code? Mar 17, 2015 at 14:13
• If you capture, say, cc_num="0346573450" as a string, rather than as the integer 346573450, then cc_num to cc_num will be the digits, in ASCII. So you'll have cc_num = 16 (the ASCII code for '0'), cc_num = 19 and so on. This is very nearly what you need - you just need to subtract 16 from each ASCII code to get the digit value. Mar 17, 2015 at 22:00
• well, in ascii table for digits 0-9 designed 48-57 numbers so in this case i think it`s gonna be like this: cc_num = 48; cc_num = 51; am i right? Mar 18, 2015 at 9:02
• Yes you are right - I managed to misread the ASCII table! Subtract 48 not 16. Mar 18, 2015 at 16:38