# Why is the last digit of my credit card number wrong in hacker1?

I was working on the hacker version of pset1 and I was using a for loop to grab the values of every single individual number (in CC#) and transfer it to an array. The for loop works fine, except that it absolutely butchers the final number. Ex: 378282246310005 is seen by the array as 37828224631000

The for loop in question does the following:

1. It takes the CC# and divides it by 10CC#.length - loopCounter (initialized at 1) and places that value in an int (curVal).
2. Does the same operation (CC# divided by 10CC#.length - loop counter) then minus the int curVal) and multiplies that entire value (incl. minus curVal) and multiplies it by 10CC#.length - loopCounter. It then places that value in a variable (long long) called num.
3. It assigns the curVal to an array called numbers like so:
numbers [loopCounter] equals curVal.

Sorry if wording above is confusing. I guess I could clarify by PM'ing someone the actual code. Thanks for any help.

I've tried appending a value to the CC# so that the butchering only happens in a final, useless digit. That only lead to another problem (3782822463100050 becomes 3782822463100049)

I've "duct-taped" the issue by appending a 9 to the CC# that the user inputs (so that the second to last digit does not round down).

`pow()` returns a double and so does `floor()`. Working with `double`s is not the best thing to do since they're not precise. You could lose some data and we don't really have to work with them at all here.

I see you're over-complicating things a little bit. You don't really need all that maths to get an array of integers. An easy way to do that is to convert the long long you've received from the user to a `string` using `sprintf()`. Keep in mind that subtracting `'0'` (the `char`) from any `char` digit gives you the real `int` value of that digit. For example,

``````string number = "1234567891234";
printf("%d\n", (number[0] - '0')); // prints the "int" 1
printf("%d\n", (number[2] - '0')); // prints the "int" 3
// and so on
``````

If you wanna follow the more-math way, well, you may use a combination of the remainder and division operators. For example,

``````long long number = 1234567891234;
printf("%d\n", (number % 10)); // prints 4
printf("%d\n", (number % 100)); // prints 34

number /= 10;
printf("%lld\n", number); // prints 123456789123
// and so on
``````

Following any of these approaches, you may either create an array of integers, fill it with these extracted digits and deal with them or deal with them directly.

• I started this 2 days ago, and went with the "more math" way of using modulo, since I was confused by the printf way I found elsewhere. Here's what I don't understand: what is the use of printing a value? How do you get from printing it to storing it in an array? Another comment: Your answer says to use "sprintf()" but your example uses "printf()". Is this a mistake? Commented Feb 11, 2016 at 23:28
• @Dr.Queso we don't need to print out anything except the type of the card or `INVALID` as instructed per the specs. the `printf` calls in the answer are only for demonstration purposes (i.e., to see the result of an operation). also `printf` and `sprintf` are different functions that do different things. consult their man pages for more!
– kzidane
Commented Feb 12, 2016 at 6:14
• Agreed that we don't need to print things; that's why I found your answer confusing, as all it does is print various things but creates not a single string or array. I see that sprintf prints to a new string, while printf prints to the terminal. sprintf was my initial approach to the problem, but the use of sprintf seems to require an understanding of something called "pointers" which seems beyond the intended scope of pset1... it seemed easier to me to just use more math :) Commented Feb 12, 2016 at 10:11