0

I'm not able to take out the second digit of the credit number, how to do it for large numbers like 16 digit,15digit,14 digit?

1 Answer 1

0

The modulo operator (%) gives you access to the last digit of a number, floor division (/) enables you to "chop off" digits from the end. Here is a minimal example:

#include <stdio.h>

int main(void)
{
    int card, right_digit, floor_1, floor_2;

    card = 12345;
    right_digit = card % 10;
    floor_1 = card / 10;
    floor_2 = card / 100;

    printf("%i, %i, %i, %i\n", card, right_digit, floor_1, floor_2);
}

Output:

12345, 5, 1234, 123

In order to traverse the card number from the right and get at every second number, you will need to combine the modulo operator with floor division in some kind of loop.

You must log in to answer this question.

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