How to resolve the algorithm Luhn test of credit card numbers step by step in the C# programming language
How to resolve the algorithm Luhn test of credit card numbers step by step in the C# programming language
Table of Contents
Problem Statement
The Luhn test is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits. Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test:
For example, if the trial number is 49927398716:
Write a function/method/procedure/subroutine that will validate a number with the Luhn test, and use it to validate the following numbers:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Luhn test of credit card numbers step by step in the C# programming language
Luhn Algorithm Implementation
The Luhn algorithm is used to check the validity of credit card numbers. It is based on the following steps:
- Double every other digit starting from the right-hand side.
- Add up all the digits in the resulting number.
- If the sum is divisible by 10, the credit card number is valid.
C# Implementation 1:
public static class Luhn
{
public static bool LuhnCheck(this string cardNumber)
{
return LuhnCheck(cardNumber.Select(c => c - '0').ToArray());
}
private static bool LuhnCheck(this int[] digits)
{
return GetCheckValue(digits) == 0;
}
private static int GetCheckValue(int[] digits)
{
return digits.Select((d, i) => i % 2 == digits.Length % 2 ? ((2 * d) % 10) + d / 5 : d).Sum() % 10;
}
}
Explanation:
This implementation of the Luhn algorithm takes a string representing the credit card number as input and converts it to an array of integers. It then applies the Luhn algorithm to the array of integers and returns true if the credit card number is valid, otherwise false.
C# Implementation 2:
public class CreditCardLogic
{
static Func<char, int> charToInt = c => c - '0';
static Func<int, int> doubleDigit = n => (n * 2).ToString().Select(charToInt).Sum();
static Func<int, bool> isOddIndex = index => index % 2 == 0;
public static bool LuhnCheck(string creditCardNumber)
{
var checkSum = creditCardNumber
.Select(charToInt)
.Reverse()
.Select((digit, index) => isOddIndex(index) ? digit : doubleDigit(digit))
.Sum();
return checkSum % 10 == 0;
}
}
Explanation:
This implementation of the Luhn algorithm is more concise and uses lambda expressions to define the necessary functions. It takes a string representing the credit card number as input and returns true if the credit card number is valid, otherwise false.
C# Implementation 3:
public static partial class Algoritmhs
{
public static bool CheckLuhn(long n)
{
int s1 = n.Shatter(true).Subset(2).Arithmetic('+');
int s2 = n.Shatter(true).Subset(1, -1, 2).ArithmeticRA('*', 2).ShatterAndSum().Arithmetic('+');
return (s1 + s2) % 10 == 0 ? true : false;
}
}
Explanation:
This implementation of the Luhn algorithm uses custom extension methods to define the necessary operations. It takes a long integer representing the credit card number as input and returns true if the credit card number is valid, otherwise false.
Source code in the csharp programming language
public static class Luhn
{
public static bool LuhnCheck(this string cardNumber)
{
return LuhnCheck(cardNumber.Select(c => c - '0').ToArray());
}
private static bool LuhnCheck(this int[] digits)
{
return GetCheckValue(digits) == 0;
}
private static int GetCheckValue(int[] digits)
{
return digits.Select((d, i) => i % 2 == digits.Length % 2 ? ((2 * d) % 10) + d / 5 : d).Sum() % 10;
}
}
public static class TestProgram
{
public static void Main()
{
long[] testNumbers = {49927398716, 49927398717, 1234567812345678, 1234567812345670};
foreach (var testNumber in testNumbers)
Console.WriteLine("{0} is {1}valid", testNumber, testNumber.ToString().LuhnCheck() ? "" : "not ");
}
}
using System;
using System.Linq;
namespace Luhn
{
class Program
{
public static bool luhn(long n)
{
long nextdigit, sum = 0;
bool alt = false;
while (n != 0)
{
nextdigit = n % 10;
if (alt)
{
nextdigit *= 2;
nextdigit -= (nextdigit > 9) ? 9 : 0;
}
sum += nextdigit;
alt = !alt;
n /= 10;
}
return (sum % 10 == 0);
}
public static bool luhnLinq(long n)
{
string s = n.ToString();
return s.Select((c, i) => (c - '0') << ((s.Length - i - 1) & 1)).Sum(n => n > 9 ? n - 9 : n) % 10 == 0;
}
static void Main(string[] args)
{
long[] given = new long[] {49927398716, 49927398717, 1234567812345678, 1234567812345670};
foreach (long num in given)
{
string valid = (luhn(num)) ? " is valid" : " is not valid";
Console.WriteLine(num + valid);
}
}
}
}
using System;
namespace Luhn_Test
{
public static class Extensions
{
public static string Reverse(this string s )
{
char[] charArray = s.ToCharArray();
Array.Reverse( charArray );
return new string( charArray );
}
}
class Program
{
public static bool Luhn(long x)
{
long s1=0;
long s2=0;
bool STATE=x%10!=0; // If it ends with zero, we want the order to be the other way around
x=long.Parse(x.ToString().Reverse());
while (x!=0)
{
s1+=STATE?x%10:0;
s2+=STATE?0:((x%10)*2>9)?(((x%10)*2/10)+((x%10)*2)%10):((x%10)*2);
STATE=!STATE; //Switch state
x/=10; //Cut the last digit and continue
}
return ((s1+s2)%10==0); //Check if it ends with zero, if so, return true, otherwise,false.
}
public static void Main(string[] args)
{
long[] ks = {1234567812345670, 49927398717, 1234567812345678 ,1234567812345670 };
foreach (long k in ks)
{
Console.WriteLine("{0} is {1} Valid.",k,Luhn(k)?"":"Not");
}
Start:
try {
Console.WriteLine("Enter your credit:");
long x=long.Parse(Console.ReadLine());
Console.WriteLine("{0} Valid.",Luhn(x)?"":"Not");
goto Start;
}
catch (FormatException)
{
goto Start;
}
}
}
}
using System;
using System.Linq;
public class CreditCardLogic
{
static Func<char, int> charToInt = c => c - '0';
static Func<int, int> doubleDigit = n => (n * 2).ToString().Select(charToInt).Sum();
static Func<int, bool> isOddIndex = index => index % 2 == 0;
public static bool LuhnCheck(string creditCardNumber)
{
var checkSum = creditCardNumber
.Select(charToInt)
.Reverse()
.Select((digit, index) => isOddIndex(index) ? digit : doubleDigit(digit))
.Sum();
return checkSum % 10 == 0;
}
}
using System;
using EuropaRTL.Utilities;
public static partial class Algoritmhs
{
public static bool CheckLuhn(long n)
{
int s1 = n.Shatter(true).Subset(2).Arithmetic('+');
int s2 = n.Shatter(true).Subset(1, -1, 2).ArithmeticRA('*', 2).ShatterAndSum().Arithmetic('+');
return (s1 + s2) % 10 == 0 ? true : false;
}
}
class Program
{
static void Main(string[] args)
{
long[] ll = {
49927398716,
49927398717,
1234567812345678,
1234567812345670
};
foreach (var item in ll)
{
item.ToString().WriteLine();
Algoritmhs.CheckLuhn(item).ToString().WriteLine();
}
Console.ReadKey();
}
}
You may also check:How to resolve the algorithm Classes step by step in the Bracmat programming language
You may also check:How to resolve the algorithm MD4 step by step in the Lua programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Factor programming language
You may also check:How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the Crystal programming language
You may also check:How to resolve the algorithm Factors of a Mersenne number step by step in the Perl programming language