Skip to content

Instantly share code, notes, and snippets.

@playerx
Created December 8, 2015 11:10
Show Gist options
  • Save playerx/aac99e57e1598a8b6724 to your computer and use it in GitHub Desktop.
Save playerx/aac99e57e1598a8b6724 to your computer and use it in GitHub Desktop.
Mod10 algorithm function on c#
private int Mod10Check(string creditCardNumber)
{
//// 1. Starting with the check digit double the value of every other digit
//// 2. If doubling of a number results in a two digits number, add up
/// the digits to get a single digit number. This will results in eight single digit numbers
//// 3. Get the sum of the digits
int sumOfDigits = creditCardNumber.Where((e) => e >= '0' && e <= '9')
.Reverse()
.Select((e, i) => ((int)e - 48) * (i % 2 == 0 ? 1 : 2))
.Sum((e) => e / 10 + e % 10);
//// If the final sum is divisible by 10, then the credit card number
// is valid. If it is not divisible by 10, the number is invalid.
return sumOfDigits % 10;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment