Created
October 7, 2018 15:10
-
-
Save cosminpopescu14/ceb98b909aa69c7424d3deb8a8e4305c to your computer and use it in GitHub Desktop.
Implementation of Luhn algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Luhn | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(IsValid()); | |
Console.ReadLine(); | |
} | |
static bool IsValid(string cardNumber = "1111222233334444") | |
{ | |
int sum = 0; | |
int numberOfDigits = cardNumber.Length; | |
int parity = (numberOfDigits - 1) % 2; | |
char[] digit = new char[] { '\0' }; | |
for (int index = numberOfDigits; index > 0; index--) | |
{ | |
digit[0] = cardNumber[index - 1]; | |
int nDigit = int.Parse(digit); | |
if (parity == index % 2) | |
nDigit *= 2; | |
sum += nDigit / 10; | |
sum += nDigit % 10; | |
} | |
return 0 == sum % 10; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment