The Luhn Check Digit Algorithm in C |
The Luhn Check Digit Algorithm in CThis program, presented in C source code form, will perform this math for you. Feed it all but the last digit of your credit card number, and it will give you the last digit. If it gives you a last digit different from the one you have, you have an invalid credit card number. #include <stdio.h> /* * Return last digit of a bank card (e.g. credit card) * Receives all the digits, but the last one as input * By Diomidis Spinellis <dds@doc.ic.ac.uk> */ int bank (u) char *u; { register i, s = 0; int l, t; l = strlen(u); for(i = 0; i < l ; i++) { t = (u[l - i - 1] - '0') * (1 + ((i + 1) % 2)); s += t < 10 ? t : t - 9; } return 10 - s % 10; } void main (argc, argv) int argc; char **argv; { while (--argc) printf ("%d\n", bank (*++argv)); } |
Discuss The Luhn Check Digit Algorithm in C in the forums.
You need to login or register to post comments.


