2

I'm having a hard time trying to find the 2nd to last digit of the user's credit card number, since that's the starting point for Luhn's algorithm. Is there a way to convert a long to an array and then get the 2nd to last position of that array? I was able to find the last digit:

long long l = 12345686868;
int last_digit = (l % 10);
printf("%i\n ", last_digit);
18

An array is not strictly required to solve this.

You already have the mechanism to extract the last digit from a number. You just need to extend this by using use division to "shift" the digits the required number of places.

For example, if you have the number 1234, to get the second digit you can divide by 10, to get 123. Using 123 mod 10 will give you the digit 3.

To get the third and fourth digits you would just need to divide by 100 and 1000 respectively.

Note the pattern here. To get the digit at position 2 you divide by 100, which is 10 raised to the power of 2, or (10^2). 1000 is 10 to the 3 or (10^3), and so on.

Or more succinctly, you can expose the nth digit by dividing by the nth power of 10.

Another different solution would be to use sprintf to convert the number into a string. This hasn't been covered yet at this point in the course, but if you have some prior experience (or are feeling brave) then you might want to try that approach. Use man sprintf from the command line to see how this might work.

| improve this answer | |
  • So I got the second to last digit and from there onwards. Separating the products' digits and adding them up is giving me trouble. For example for this credit card number: (1234567), 12, 8, and 4 would be the result of multiplying every other digit by 2. How would you add the digits so that it's in a form of: 1 + 2 + 8 + 4? I'm thinking again in putting the resulting numbers into an array. Although, I don't know how that would access '1' and '2' in the number '12'. – Arun Suresh Jul 31 '14 at 19:12
  • You could loop through the digits and use the above algorithms to extract the digits. The trick is figuring out how many digits are in the number, which can be done using a loop, if/else structure, or a logarithm. Open another question and I can provide more info. – Luke Van In Jul 31 '14 at 20:30
  • Opened another question, thanks! – Arun Suresh Jul 31 '14 at 20:48
  • I did use sprintf , succeeded, but found no discussion about this on the web (reddit, facebook,...) until now. Just started the course Is sprintf the most efficient yet? – Hieu Do Mar 11 '15 at 4:03
5

You can use arithmetic to loop through the numbers.

In your example:

12345686868 % 10 = 8

which leads on to

12345686868 / 10 = 1234568686

so

1234568686 % 10 = 6

You can carry this on to access to all the numbers in the credit card.

| improve this answer | |
  • But in this way you have accessed all the numbers on the credit card not every second digit according to Luhn's algorithm. – Shahroz Ali Dec 26 '19 at 19:36
  • Well, you need all digits to calculate the check digit. It’s up to you to figure out how to use the arithmetic to treat every other digit as needed. – curiouskiwi Dec 26 '19 at 23:27
  • Yeah ! Sure... Thanks – Shahroz Ali Dec 27 '19 at 9:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .